Search Unity

UnityGUI performance tweaks and tips?

Discussion in 'Immediate Mode GUI (IMGUI)' started by bigkahuna, Dec 28, 2007.

  1. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    My current project is making extensive use of UnityGUI and is taking a pretty huge performance hit (drops from 78 fps to 50 fps). So I thought I'd start a thread to solicit tips and tweaks to help speed up UnityGUI performance. So in the spirit of the holidays, I'll go first (I actually learned this from Daniel Brauer on the Alpha List):

    Instead of writing a series of GUILayout instructions like this:
    Code (csharp):
    1. GUILayout.Box ("Title");
    2. GUILayout.Button ("Button One");
    3. GUILayout.Button ("Button Two");
    You can import GUILayout from the UnityEngine and just call the instructions, like so:
    Code (csharp):
    1. import UnityEngine.GUILayout;
    2.  
    3. Box ("Title");
    4. Button ("Button One");
    5. Button ("Button Two");
    Anyone else have some UnityGUI tweaks / tips to share?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    That'll save typing, but won't have any noticeable effect on performance. Since the GUI stuff is done in code, I suppose the usual tips for optimizing code apply here as well (probably more so, since OnGUI functions run more often than Update).

    --Eric
     
  3. nafonso

    nafonso

    Joined:
    Aug 10, 2006
    Posts:
    377
    One thing that will probably help you get some frames is to not abuse of Matrix multiplications (although I think you will only really notice it after a lot of elements)

    Instead of doing things like:
    Code (csharp):
    1. GUI.matrix *= Matrix4x4.TRS( new Vector3(30,30,0), ..., ... );
    2. GUI.Box(new Rect(0,0,30,30),"Box");
    try using something like:
    Code (csharp):
    1. GUI.Box(new Rect(30,30,30,30), "Box");
    I haven't really tested the performance improvement from doing this, but with a lot (and I mean A LOT) of elements with matrix multiplications I did find it to push some frames (i.e. perhaps 5 to 10fps). Could have been just some lucky chance :p

    Regards,
    Afonso
     
  4. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    This is probably obvious, but keep your calculations out of the OnGUI function.
    Since OnGUI can be called multiple times per frame, anything that is not dependent on keeping time with the GUI shouldn't be there.
    Basically Update() and OnGUI() are not interchangeable even though it may seem so.

    I'm pretty sure GUI.Layout would be slightly slower than GUI since it has to calculate the layout in more detail, rather than just throwing everything in based on it's Rect parameter. Nicholas would be the one to ask though.