Search Unity

Centre buttons in a GUI window C#

Discussion in 'Immediate Mode GUI (IMGUI)' started by eggfriedbryce, Apr 17, 2014.

  1. eggfriedbryce

    eggfriedbryce

    Joined:
    Apr 8, 2013
    Posts:
    4
    Hello,
    I was wondering if anyone could help me with a problem regarding Unity GUI in C#.

    I have made a GUI.Window: Image


    Code (csharp):
    1.  
    2.     void OnGUI()
    3. {
    4. bottompanelRect3 = GUI.Window(3, bottompanelRect3, bottompanelFunc3, "Region")
    5. }
    6.  
    7.  
    Function:

    Code (csharp):
    1.  
    2. void bottompanelFunc3(int id)
    3.         {
    4.             GUILayout.BeginHorizontal();
    5.  
    6.         if (GUILayout.Button (regionImage, GUILayout.Width(40), GUILayout.Height(40)))
    7.             {
    8.             }
    9.         if (GUILayout.Button (capitalImage, GUILayout.Width(40), GUILayout.Height(40)))
    10.             {
    11.             }
    12.  
    13.             GUILayout.EndHorizontal ();
    14.  
    15.         }
    16.  
    I would like to align the buttons in the centre of the window and not to the left like they are now. I would also like to know how to align the windows title "Region" to the left as well so that I can experiment with what looks good.

    Thanks in advance.

    edit: Thanks Patico and Ghidera
     
    Last edited: Apr 22, 2014
  2. Patico

    Patico

    Joined:
    May 21, 2013
    Posts:
    886
    Use FlexibleSpace before and after yor buttons. something like this:
    Code (csharp):
    1.  
    2. void bottompanelFunc3(int id){
    3.    GUILayout.BeginHorizontal();
    4.  
    5.    GUILayout.FlexibleSpace();
    6.  
    7.    if (GUILayout.Button (regionImage, GUILayout.Width(40), GUILayout.Height(40))){
    8.    }
    9.    if (GUILayout.Button (capitalImage, GUILayout.Width(40), GUILayout.Height(40))){
    10.    }
    11.  
    12.    GUILayout.FlexibleSpace();
    13.  
    14.    GUILayout.EndHorizontal ();
    15. }
    16.  
     
    jister likes this.
  3. Ghidera

    Ghidera

    Joined:
    Mar 6, 2013
    Posts:
    53
    On a marginally related note, if your GUI is more than a few lines it'll start getting hard to find specific things due to the lack of indenting. Somewhere on the forums I saw a tip that really makes things easier to manage: Use braces! Although the gui areas/windows etc don't use them, you can add them in for indentation anyway - like this:

    Code (csharp):
    1.  
    2. void bottompanelFunc3(int id){
    3.     GUILayout.BeginHorizontal(); {
    4.         GUILayout.BeginVertical(); {
    5.  
    6.             GUILayout.FlexibleSpace();
    7.  
    8.             if (GUILayout.Button (regionImage, GUILayout.Width(40), GUILayout.Height(40))){
    9.             }
    10.             if (GUILayout.Button (capitalImage, GUILayout.Width(40), GUILayout.Height(40))){
    11.             }
    12.  
    13.             GUILayout.FlexibleSpace();
    14.         }
    15.     }
    16.     GUILayout.EndHorizontal ();
    17. }
    I added the vertical just for demonstation purposes.