Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'Scripting' started by infinitypbr, Aug 18, 2014.

  1. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    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!!

    Code (csharp):
    1.  
    2. functionOnGUI() {
    3. if (barAlpha != 0.0 && !paused)
    4.  {
    5. barPos = playerCamera.WorldToScreenPoint (transform.position + (transform.up * heightOffset));
    6. startX = Mathf.Floor(barPos.x - (barLength / 2));
    7. startY = Mathf.Floor(Screen.height - barPos.y);
    8. if (hpRatioCurrent != 0)
    9.  {
    10. GUI.color = Color(1,1,1,alphaToUse * barAlpha);
    11. GUI.DrawTexture (Rect (startX,startY,barLength,barHeight),hpTexture, ScaleMode.StretchToFill);
    12. GUI.color = Color(1,1,1,1 * barAlpha);
    13. GUI.DrawTexture (Rect (startX,startY,barLength * hpRatioCurrent,barHeight),hpTexture, ScaleMode.StretchToFill);
    14.  }
    15. else
    16.  {
    17. GUI.color = Color(1,1,1,alphaToUse * barAlpha);
    18. GUI.DrawTexture (Rect (startX,startY,barLength,barHeight),deadTexture, ScaleMode.StretchToFill);
    19. GUI.color = Color(1,1,1,1 * barAlpha);
    20. GUI.DrawTexture (Rect (startX,startY,barLength * hpRatioCurrent,barHeight),deadTexture, ScaleMode.StretchToFill);
    21.  }
    22.  }
    23. }
    24.  
     
  2. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    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)?
     
  3. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    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
     
  4. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    Ah, hopefully the new gui will get released wednesday! :D