- Home /
Whats an efficient method for drawing hundreds of Health Bars etc. over units for an RTS?
That's the whole question really, I have been using:
rectTransform.position = Camera.main.WorldToScreenPoint(unit.transform.position);
In the UI elements update event, but it slows down a lot with just that line, once you have 20-30 units. I've tried putting them all on one canvas, and giving each a seperate canvas, but it appears to be the WorldToScreenPoint function causing the slowdown.
There's no real way to call this less than in Update either, at in addition to the unit moving (which you could dead reckon a bit) the screen position and zoom level may change.
Any ideas? This must be have been solved before?
Answer by Overcast · Jun 12, 2015 at 01:24 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.
Answer by Eno-Khaon · Jun 12, 2015 at 04:01 AM
I can't say I know for sure what the most efficient approaches are, but I can tell you one thing: The GUI is spectacularly inefficient.
My first thought would be to use 3D objects (planes) with a billboard shader for a baseline. With this, you can have a baseline to work from. From there, some tweaks to initial sizing and general re-scaling of parts should make for reasonably usable health bars.
hmmm that's a shame, as I was really hoping to use the new UI elements in game to shortcut things.
Thanks for the advice, anywhere I could go to clarify with Unity that their UI is indeed a bad choice for this sort of thing?
Generally, it's rather easy to try for yourself.
For either GUI system, display a large quantity of text objects (for the old GUI especially, it only takes 20 to 30 labels to drop from ~250 to ~45 fps, for instance). The increase in draw calls can grow rather quickly, and the more complex the scene (i.e. tons of units needing health displays each), the greater the cost of using the UI.
Your answer
Follow this Question
Related Questions
UI-based game memory and performance issue. 1 Answer
Heavy use of layout groups and content fitters performance 1 Answer
Whats the most efficient method for placing UI elements at world positions? (Above units etc.) 2 Answers
UI Text Optimization? Updating Geometry seems to be slow 0 Answers
Sprite Alpha Performance 0 Answers