Search Unity

Centering GUI?

Discussion in 'Immediate Mode GUI (IMGUI)' started by Joey_Thiesen, Nov 27, 2011.

  1. Joey_Thiesen

    Joey_Thiesen

    Joined:
    Nov 25, 2011
    Posts:
    65
    I've read articles, books, and various other forms of documentation, but can't for the life of me figure out how to center GUI inputs regardless of resolution.

    Any ideas?
     
  2. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,085
    Use Screen.height and Screen.width when placing your GUI elements.
     
  3. SomeGuy22

    SomeGuy22

    Joined:
    Jun 3, 2011
    Posts:
    722
    For most GUI functions, you need to give a Rect() function, a rectangle.

    Within the Rect(), function, you need to give 4 parameters -- x position, y position, x width, y width.

    There is a variable out there called Screen.width and Screen.height.

    These variables hold the key to perfect GUI. :p

    Essentially, you want, say, a GUI.Label() to be in the center of the screen, taking up the space of half the screen for width, and half for height.

    If your Rect(), you want half the x resolution of the screen as your first variable, so you would type:

    Code (csharp):
    1.  
    2. .5 * Screen.width
    3. //or
    4. Screen.width / 2
    5.  
    The next would be your y resolution:

    Code (csharp):
    1.  
    2. .5 * Screen.height
    3. //or
    4. Screen.height / 2
    5.  
    Then you have size. This should mostly be about the same:

    Code (csharp):
    1.  
    2. .5 * Screen.width, .5 * Screen.height
    3.  
    All together, you have your Rect() function looking like this:

    Code (csharp):
    1.  
    2. Rect(.5 * Screen.width, .5 * Screen.height, .5 * Screen.width, .5 * Screen.height)
    3.  
    And then your example Label() script would be:

    Code (csharp):
    1.  
    2. function OnGUI ()
    3. {
    4. GUI.Label(Rect(.5 * Screen.width, .5 * Screen.height, .5 * Screen.width, .5 * Screen.height), "I'm in the center!!!");
    5. }
    6.  
    Hope this helps :)
     
  4. Joey_Thiesen

    Joey_Thiesen

    Joined:
    Nov 25, 2011
    Posts:
    65
    Alright. Thanks guys. Think I got it.
     
  5. SNS_Case

    SNS_Case

    Joined:
    Oct 7, 2011
    Posts:
    58
    This has mostly been resolved, but just throwing out there that you'll want to offset the GUI element by half of its width/height in the first two parameters if you want the center of the GUI to be at the center of the screen. Simply using Screen.width(or height) / 2 places its origin (upper left corner) at the center of the screen. So...

    Code (csharp):
    1.  
    2. GUI.Label(new Rect((Screen.width / 2) - Screen.width / 4, (Screen.height / 2) - Screen.height / 4, Screen.width / 2, Screen.height / 2), "Label text");
    3.  
    That would place the GUI element centered on your screen. On a side note, if you're really itching for that extra nth of saved compute time, then go ahead and use multiplication, but I find it's easier to read division.
     
  6. Chuckler

    Chuckler

    Joined:
    Mar 15, 2012
    Posts:
    4
    It doesnt seem to work for me...it starts in the center tho.
    then there seems to be a difference in position when using gui.button gui.label and gui.box each giving me a different position with the same co ordinates...
    I wonder how i can space up and down in an orderly fashion as well...this should be so simple, like basic, 5 minutes and be able to move on
     
  7. dkozar

    dkozar

    Joined:
    Nov 30, 2009
    Posts:
    1,410
    Here, I've created the simplest example... I think it's self-explanatory. :)

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. /// <summary>
    4. /// Centered GUI menu example
    5. /// </summary>
    6. /// <remarks>Author: Danko Kozar</remarks>
    7. public class GuiMenu : MonoBehaviour
    8. {
    9.     public Rect ButtonSize = new Rect(0, 0, 150, 60);
    10.     public float GapSize = 10;
    11.     public string[] Buttons = new[] { "Option 1", "Option 2", "Option 3", "Option 4" };
    12.     public GUISkin Skin;
    13.  
    14.     private Rect _menuBounds;
    15.    
    16.     void OnGUI()
    17.     {
    18.         int count = Buttons.Length;
    19.         float width = ButtonSize.width; // the width of a single button
    20.         float height = ButtonSize.height * count + GapSize * Mathf.Max(count-1, 0); // sum of button heights and gaps
    21.         float x = (Screen.width - width)*0.5f;
    22.         float y = (Screen.height - height)*0.5f;
    23.  
    24.         _menuBounds = new Rect(x, y, width, height);
    25.  
    26.         if (null != Skin) // apply skin
    27.             GUI.skin = Skin;
    28.  
    29.         GUI.BeginGroup(_menuBounds); // group start
    30.  
    31.         int index = 0;
    32.         foreach (string s in Buttons)
    33.         {
    34.             if (GUI.Button(new Rect(0, (ButtonSize.height + GapSize) * index, ButtonSize.width, ButtonSize.height), s)) // relative to group, so x and y start from 0
    35.             {
    36.                 ClickHandler(index);
    37.             }
    38.             index++;
    39.         }
    40.  
    41.         GUI.EndGroup(); // group end
    42.     }
    43.  
    44.     void ClickHandler(int index)
    45.     {
    46.         Debug.Log("Clicked button at index " + index);
    47.     }
    48. }
     

    Attached Files:

    Last edited: Jan 29, 2013
    tokar_dev likes this.
  8. Burton-Radons

    Burton-Radons

    Joined:
    Jan 27, 2014
    Posts:
    1
    For the sake of people coming here from Google, here is the way to make a GUI fully automatically centred horizontally and vertically on the screen without having to measure it or guess (this may well not have worked at the time). You need to wrap the content with this:

    Code (csharp):
    1. GUILayout.BeginArea(new Rect(0, 0, Screen.width, Screen.height));
    2. GUILayout.FlexibleSpace();
    3. GUILayout.BeginHorizontal();
    4. GUILayout.FlexibleSpace();
    5.  
    6. // Now you can finally put in your GUI, such as:
    7. GUILayout.BeginVertical("box");
    8. GUILayout.Button("Hello, world!");
    9. GUILayout.EndVertical();
    10.  
    11. GUILayout.FlexibleSpace();
    12. GUILayout.EndHorizontal();
    13. GUILayout.FlexibleSpace();
    14. GUILayout.EndArea();
    What's happening is that the GUILayout.FlexibleSpace() is being used on the sides of both a horizontal and vertical section, which makes them take up all of the space. If you want to align your GUI at a different position, you can remove one of the GUILayout.FlexibleSpace()s or replace it with a GUILayout.Space(float pixels) to put a margin between the GUI and the edge of the screen. Removing or replacing both the GUILayout.FlexibleSpace() calls from one direction causes the GUI to be stretched over the whole screen horizontally or vertically.
     
    Last edited: Jan 27, 2014
    tokar_dev and travlake like this.