GUI.Repaint taking extra time when two or more health bars are drawn… (Draw Texture)

Hello!

I’ve found that when I draw two or more enemy health bars (which fade out after a few seconds) at the same time, GUI.Repaint takes up a bunch more time than when just one is drawn. Drawing many more than 2 doesn’t seem to take much more time than just 2, however.

Is there a better way to do health bars? I’m pasting the OnGUI code below, along with a profiler screenshot.
Thanks!!

functionOnGUI() {
if (barAlpha != 0.0 && !paused)
 {
barPos = playerCamera.WorldToScreenPoint (transform.position + (transform.up * heightOffset));
startX = Mathf.Floor(barPos.x - (barLength / 2));
startY = Mathf.Floor(Screen.height - barPos.y);
if (hpRatioCurrent != 0)
 {
GUI.color = Color(1,1,1,alphaToUse * barAlpha);
GUI.DrawTexture (Rect (startX,startY,barLength,barHeight),hpTexture, ScaleMode.StretchToFill);
GUI.color = Color(1,1,1,1 * barAlpha);
GUI.DrawTexture (Rect (startX,startY,barLength * hpRatioCurrent,barHeight),hpTexture, ScaleMode.StretchToFill);
 }
else
 {
GUI.color = Color(1,1,1,alphaToUse * barAlpha);
GUI.DrawTexture (Rect (startX,startY,barLength,barHeight),deadTexture, ScaleMode.StretchToFill);
GUI.color = Color(1,1,1,1 * barAlpha);
GUI.DrawTexture (Rect (startX,startY,barLength * hpRatioCurrent,barHeight),deadTexture, ScaleMode.StretchToFill);
 }
 }
}

Follow up question: Would it be faster/better to make the same effect with primitives (cube/capsule) shown in my GUI camera (so flat, isometric, and just following the position of the enemy)?

you should avoid using built in gui. its rubbish.

use it for development only, then replace with something like NGUI… or wait for the new Unity GUI

Ah, hopefully the new gui will get released wednesday! :smile: