I need an efficient method for drawing Healthbars etc, preferrably using the new UI

I’ve asked this question in Answers, but got no really useful direction, I’d love an answer from Unity as to the performance of this, and any suggestions for how I should be implementing it best practice or otherwise.

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 separate canvas (per a suggestion in Answers to use a separate canvas for each), but it appears to be the WorldToScreenPoint function causing the slowdown.

Ideally I need to draw up to 100 of theses on screen at a time, each following a unit or building (where the case might be the view scrolling or zooming, and I cant really progress until I know there is A WAY to do this efficiently.

Any thoughts are welcome - please!

And after all that I have found the culprit - Cross posting for future optimizers:

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.

1 Like