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);
}
}
}