- Home /
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?
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.
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.
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).
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.
Nice catch!
I really, really hope that Camera.main isn't just a wrapper around FindObjectWithTag("$$anonymous$$ainCamera"), because that would be insane.
@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.
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.
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.
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.