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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Performance issues just because of Health Display?

Discussion in 'Scripting' started by dasschlumpf, Apr 29, 2015.

  1. dasschlumpf

    dasschlumpf

    Joined:
    Apr 29, 2015
    Posts:
    7
    Hello folks!
    I hope this is the right section.

    Lately i am facing a wiered problem which i can't solve by myself.


    As you can see my health display is taking 20 % (sometimes 30%) of the overall performance.
    Its just displaying the current health on screen with this code:
    Code (CSharp):
    1.  
    2.         if (isPlayer == true) {
    3.             Debug.Log ("Health Display");
    4.         //Health Texture
    5.             GUI.depth = 2;
    6.             GUI.DrawTexture(new Rect (0,Screen.height - 25,75,25), HealthBar);
    7.  
    8.         //HitPoints
    9.             GUI.depth = 1;
    10.             GUILayout.BeginArea (new Rect (0,Screen.height - 23,97,150));
    11.             GUILayout.BeginHorizontal ();
    12.             GUILayout.FlexibleSpace ();
    13.             GUILayout.Button (""+currentHitPoints, customHealth);
    14.             GUILayout.FlexibleSpace ();
    15.             GUILayout.EndHorizontal ();
    16.             GUILayout.EndArea ();  
    17.  
    18.         //Crosshair
    19.             GUI.DrawTexture(new Rect ((Screen.width - size)/2 , (Screen.height - size)/2, size, size), crosshair);



    Any ideas?
    Greetings dasschlumpf
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Is there anything else going on in the game? It could just be that it's taking the most time because there's nothing really happening. Are you actually dropping frames? What's your FPS?
     
  3. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,534
    What part of this OnGUI method is taking the most time? Can you comment out sections (such as Health Textures and Crosshair) to narrow down the slow section?

    Also, do you need to concatenate an empty string? Can you replace this:
    Code (csharp):
    1. ""+currentHitPoints
    with this?
    Code (csharp):
    1. currentHitPoints.ToString()
    Or better yet cache the string value of currentHitPoints in Update so you're not doing the same string conversion multiple times every frame? I don't know if Mono is smart enough to optimize away the empty string. If not, it will generate garbage several times every frame (remember OnGUI is called multiple times per frame) and you'll have a garbage collection penalty.

    Finally, the main culprit is probably:
    Code (csharp):
    1. Debug.Log ("Health Display");
    Debug.Log() is a slow method. If you're calling it multiple times a frame, it will make everything very slow.
     
  4. dasschlumpf

    dasschlumpf

    Joined:
    Apr 29, 2015
    Posts:
    7
    There is somethign else going on in the game. Every second i am checking for a specific Tag in a radius of 10 units.
    Also there are probably more Debug.Logs going on than this one mentioned in the code.

    The average FPS i get is 55.
    I have a GeForce GTX 970 with a i7 4790k. at 4,5ghZ.

    @TonyLi thanks and i will try that when i am home again!
    I will keep you guys updated.

    dasschlumpf
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Turn off vsync. You've probably got nothing to worry about. :)
     
  6. dasschlumpf

    dasschlumpf

    Joined:
    Apr 29, 2015
    Posts:
    7
    Turning VSYNC off + removing
    Code (CSharp):
    1. Debug.Log ("Health Display");
    did the trick.
    Thank you guys :)

    Have a nice day!
    Schlumpf
     
  7. Stef_Morojna

    Stef_Morojna

    Joined:
    Apr 15, 2015
    Posts:
    289
    How do I turn it off?
     
  8. dasschlumpf

    dasschlumpf

    Joined:
    Apr 29, 2015
    Posts:
    7
    You can disable it in your Quality Settings for each Quality Level.

    Schlumpf