Search Unity

Nametags over players is extremely slow

Discussion in 'Immediate Mode GUI (IMGUI)' started by omgketchup, Apr 17, 2012.

  1. omgketchup

    omgketchup

    Joined:
    Mar 28, 2012
    Posts:
    41
    Every object that has a "Health" component will display their name over their head.

    Currently, within the "Health" script, along with managing health, I have the following:

    Code (csharp):
    1.  
    2. function OnGUI(){
    3.     var screenPos:Vector3 = Camera.mainCamera.WorldToScreenPoint(gameObject.transform.position);
    4.     GUI.Box(Rect(screenPos.x - 35, screenPos.y - 5, 70, 20), gameObject.name);
    5. }
    6.  
    This causes the game to run extremely slow, but when I turn it off, no problems. Any advice on how else to do this?
     
  2. KyleStaves

    KyleStaves

    Joined:
    Nov 4, 2009
    Posts:
    821
    A better alternative would be to use an actual mesh-based object (maybe via one of the awesome 2d frameworks like "2D Toolkit" or "EZ GUI"). However, if you have to use OnGUI the best thing would be to compress it into a single OnGUI call.

    What I did when I first started using Unity for a similar issue was have a single "manager" script with an OnGUI that would loop through and draw all my nameplates (instead of having 100 different copies all calling OnGUI).
     
    GregMeach likes this.
  3. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    How many objects are we talking about here?

    If there's a lot then OnGUI isn't a particularly effective way to do it, since it doesn't make particularly effective use of either CPU or GPU time.

    If you'd prefer to stick to OnGUI, doing as Kyle suggests and drawing all of the GUI parts from a loop in a manager object will improve things. I also strongly suggest checking Event.current.type and early-returning on events that you don't need to use.

    However, those things will only improve the CPU part of the work. The GPU part will remain just as inefficient, so if that's where your bottleneck is, you'll need to look for better solutions than OnGUI as a whole.
     
  4. timaouxQC

    timaouxQC

    Joined:
    Sep 27, 2012
    Posts:
    6
    try this :)

    just configure it to work with your script.

    Code (csharp):
    1.  
    2. var Name : String;
    3. var font : Font;
    4. var col : Color;
    5.  
    6.  
    7. function Update ()
    8. {
    9. }
    10. function OnGUI()
    11. {
    12.     screenPosition = Camera.main.WorldToScreenPoint(Vector3(transform.position.x, transform.position.y+1, transform.position.z));
    13.     screenPosition.x = Mathf.Clamp(screenPosition.x, 20, Screen.width-75);
    14.     screenPosition.y = Screen.height - screenPosition.y;
    15.     screenPosition.y = Mathf.Clamp(screenPosition.y, 50, Screen.height);
    16.    
    17.    
    18.     GUI.skin.font = font;
    19.     GUI.color = col;
    20.     GUI.Label(new Rect(screenPosition.x-10,screenPosition.y-40,100,20), Name);
    21.                
    22. }
    23.