Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Overcast · Jun 04, 2015 at 09:15 AM · performanceuser interfacertsoverlayworldtoscreenpoint

Whats the most efficient method for placing UI elements at world positions? (Above units etc.)

I need to draw GUI elements above buildings and units for an RTS. I've seen the method being used:

 rectTransform.position = Camera.main.WorldToScreenPoint(unit.transform.position);

Using the screen space overlay canvas render mode.

When implementing this, it seems very performance hungry, and needs to run in update to remain accurate when scrolling the viewport or for moving units.

So my question is, is there a more efficient way to do this?

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Baste · Jun 04, 2015 at 09:30 AM 0
Share

Have you actually profiled to see if this is the case?

I've got a bunch of text boxes polling for screen position with the method you've written, with a bunch of extra logic, and they're so far down the profiler that I can't even see them unless I scroll.

Developer advice: don't optimize ahead of time.

avatar image Eno-Khaon · Jun 04, 2015 at 09:51 AM 0
Share

As an example...

 if(myObject.tag == "Tag")
     // Very slow
 if(myObject.CompareTag("Tag"))
     // $$anonymous$$inda slow
 if(myObject.layer == layerNumber)
     // Fast

There are some things you can learn to improve on and optimize the first time around, and it saves a huge amount of hassle later on.

avatar image Overcast · Jun 04, 2015 at 09:58 AM 0
Share

Thanks Baste,

While I agree with your comment generally speaking, as this is the first Unity project I've done on the new UI, I want to make sure I'm not doing something that is going to create a huge amount of work to redo later.

I'm not after optimization here so much as best practice, and the Unity documentation is still a little sparse on the new UI best practice.

In my case, I have profiled it, which is why I know it's this exact call slowing things down (it's the only line in the class's Upadte method).

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Overcast · Jun 12, 2015 at 01:25 PM

Well I can mark this as solved. It turns out calling Camera.main essentially searched for the camera tagged appropriately every time and is INCREDIBLY slow.

Caching the main camera in a public field of a manager class fixed the problem completely.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Baste · Jun 12, 2015 at 04:44 PM 0
Share

Nice catch!

I really, really hope that Camera.main isn't just a wrapper around FindObjectWithTag("$$anonymous$$ainCamera"), because that would be insane.

avatar image valyard · Aug 13, 2015 at 04:42 AM 0
Share

@Baste, it kinda is... Because Unity doesn't know your Camera.main usage pattern. So you should cache it if you ever have only one main camera.

avatar image
1

Answer by valyard · Jun 04, 2015 at 09:48 AM

This approach seems fine.

As Baste said you should profile first. But what you need to consider is the cost of moving an UI object. When you move just one object in a Canvas it reconstructs its internal mesh representation. So you at least should move dynamic objects to a separate Canvas. Probably it would be a good idea to have a separate Canvas for every unit UI.

You also can try using World Space UI and prerendering unit UI into a texture if it doesn't change much.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Overcast · Jun 04, 2015 at 09:55 AM 0
Share

Ah, this may be the real culprit then. I am currently drawing them all to the same canvas as the main UI, thinking that would be less draw calls.

avatar image Overcast · Jun 10, 2015 at 09:52 PM 0
Share

I've had to remove this as the correct answer, as it doesn't actually improve performance at all. with 20-30 of these, the game's performance is slowed significantly.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Whats an efficient method for drawing hundreds of Health Bars etc. over units for an RTS? 2 Answers

GUI alternatives ? 1 Answer

Screenspace UI not working after build (MacOS) 1 Answer

Cheap way in making Fog of War 1 Answer

Issues with movement in an RTS Game. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges