Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Align GUILayout elements

Discussion in 'Scripting' started by UserNobody, Sep 16, 2020.

  1. UserNobody

    UserNobody

    Joined:
    Oct 3, 2019
    Posts:
    144
    Hello,
    I am working on improving a transform inspector a bit and currently got stuck.

    Is there a way to align these buttons to the right?

    upload_2020-9-17_1-48-27.png

    These are just two GUILayout buttons wrapped inside the horizontal group.

    Code (CSharp):
    1. public override void OnInspectorGUI()
    2. {
    3.     EditorGUILayout.BeginHorizontal();
    4.     GUILayout.Button("World", EditorStyles.miniButtonLeft, GUILayout.Width(85));
    5.     GUILayout.Button("Local", EditorStyles.miniButtonRight, GUILayout.Width(85));
    6.     EditorGUILayout.EndHorizontal();
    7.  
    8.     defaultEditor.OnInspectorGUI();
    9. }
    How can I position them to the right?
     
  2. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,531
    I answered your question on Stackoverflow. Though for completeness, here's it again:

    ---

    You can move your buttons to the right side by adding a flexible space in front of them:

    Code (CSharp):
    1.     public override void OnInspectorGUI()
    2.     {
    3.         EditorGUILayout.BeginHorizontal();
    4.         GUILayout.FlexibleSpace();
    5.         GUILayout.Button("World", EditorStyles.miniButtonLeft, GUILayout.Width(85));
    6.         GUILayout.Button("Local", EditorStyles.miniButtonRight, GUILayout.Width(85));
    7.         EditorGUILayout.EndHorizontal();
    8.  
    9.         defaultEditor.OnInspectorGUI();
    10.     }
    11.  
    A flexible space simply eats all left over space within a layout group. If you have multiple flexible spaces, the space is equally distributed between them.

    You might find some more details in my IMGUI crash course