Search Unity

what's the exact difference between GUI and GUILayout

Discussion in 'Immediate Mode GUI (IMGUI)' started by unitymatrix, Jan 4, 2010.

  1. unitymatrix

    unitymatrix

    Joined:
    Dec 29, 2009
    Posts:
    120
    in the reference,it said "The GUILayout class is the interface for Unity gui with automatic layouting."
    dose that means GUILayout is only design for automatic layouting? Belongs to GUI? and when to use GUI or GUILayout more properly?
     
  2. Der Dude

    Der Dude

    Joined:
    Aug 7, 2006
    Posts:
    213
    GUI and GUILayout basically do the same thing. All the controls that you create are identical in both.
    The difference is that with GUI you have to give an absolute location and size of your controls.
    With GUILayout you don't do that. Unity organizes the control positions automatically. How Unity does this can be defined by methods like GUILayout.BeginVertical, GUILayout.BeginHorizontal and GUILayout.BeginArea (just to name the most common). Also you can set desired button sizes, etc.

    I almost always use GUILayout, as it makes adapting to differenz screen sizes relatively easy.
     
    HaiderBassim04 likes this.
  3. NathanWarden

    NathanWarden

    Joined:
    Oct 4, 2005
    Posts:
    663
    In addition to what Der Dude said, I've noticed that GUILayout and the manual GUI function vary in CPU usage.

    GUILayout tends to take approx 2x as much CPU as GUI even when I'm calculating text sizes for the standard GUI function. GUILayout tends to take 3x to 4x as long if I use a GUILayoutOption at the end of it.

    I figured this out by using the Profiler.

    So, you will sacrifice some performance if you go with the GUILayout function which may or may not matter depending on the CPU intensity of your game.

    Hope this helps :)
    Nathan
     
  4. littlepoe

    littlepoe

    Joined:
    Jul 22, 2011
    Posts:
    6
    it helps a lot ,thank you all.
     
  5. Gamedevmk

    Gamedevmk

    Joined:
    Feb 27, 2015
    Posts:
    1
    Thank you for that piece of intel.
     
  6. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    This is a very old thread. If you want to create a UI for your game, it makes more sense to use the new Unity UI instead of the the old OnGUI functionality.
     
    HaiderBassim04 likes this.
  7. thesfid

    thesfid

    Joined:
    Oct 19, 2010
    Posts:
    12
    GUILayout is killing performance on my Android device. I tried using the new Unity UI for our app and it did not work well showing numbers that update every frame. My plan is to replace all my GUILayout code with GUI and hope that solves the choppiness I'm seeing. Thanks for the benchmark, NathanWarden.
     
  8. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Could you elaborate on this? The new UI works just fine for updating numbers every frame (and is leaps and bounds faster, performance-wise, than either GUI. or GUILayout)
     
  9. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,702
    For the new Unity UI, if you're experiencing performance issues on mobile, you may want to switch to a mobile shader or use jonbro5556's shader, which he posted here. Also make sure you're not disabling and re-enabling your UI element(s) or deactivating and reactivating their GameObjects, as OnEnable can be a little expensive, especially if your Text elements have Best Fit ticked. But, generally speaking, Unity UI will outperform legacy GUI, so you might want to look into it again. Also review the Unity UI Best Practices post.

    If you decide to stick with legacy GUI, keep in mind that OnGUI will be called multiple times per frame, so minimize what you do in it. For example, don't compute Rect bounds each time OnGUI is called; instead, compute it elsewhere and use a cached copy. And certainly don't do any kind of string manipulation (e.g., GUI.Label(rect, "Score: " + currentScore)) because this will generate garbage every frame, causing GC spikes.
     
  10. thesfid

    thesfid

    Joined:
    Oct 19, 2010
    Posts:
    12
    It's been a few months since encountering the problem, but from what I can recall the problem came from trying to display data from iOS core GPS services. Occasionally the text in the UI elements would become garbled and unreadable, sometimes correcting itself after a few seconds and sometimes not. We weren't able to figure out the cause before just using legacy GUI scripting.
     
  11. HaiderBassim04

    HaiderBassim04

    Joined:
    Mar 4, 2016
    Posts:
    4
    Does this still the case in current versions of Unity?
     
  12. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    If you're using this GUI system, yes; but you shouldn't be using this system except for editor code (and even for that its days are numbered). For in-game UI you should be using the Canvas-based system (called UGUI) instead, which is a completely different thing.
     
    HaiderBassim04 likes this.