Search code examples
c#unity-game-engineunity3d-editorunity-editor

To make last EditorGUILayout of EditorWindow fill the remaining space?


By default all EditorGUILayout.BeginHorizontal() and EditorGUILayout.BeginVertical() groups fill all available parent width (or EditorWindow width). Even just vertical groups. But at the same time height is always 0 (if there's no elements in there, otherwise the width will be equal to total inner elements height).

What should I do to force fill all available or remaining height for the element groups?

What should I do to make fill space evenly?


What do I have now:

EditorGUILayout.BeginHorizontal(); {
//          EditorGUILayout.TextField("Label", "Text");
} EditorGUILayout.EndHorizontal();
 containerRect = GUILayoutUtility.GetLastRect();

var containerRectVertical = Rect.zero;
EditorGUILayout.BeginHorizontal(); {
    EditorGUILayout.BeginVertical(); {

    } EditorGUILayout.EndVertical();

    containerRectVertical = GUILayoutUtility.GetLastRect();         
} EditorGUILayout.EndHorizontal();  
var containerRect2 = GUILayoutUtility.GetLastRect();

Debug.Log($"winSize: {position.width}x{position.height}, horizSectonSize: {containerRect.width}x{containerRect.height}, 2: {containerRect2.width}x{containerRect2.height}" +
              $", containerRectVertical: {containerRectVertical.width}x{containerRectVertical.height}");

Output:

winSize: 446x192, horizSectonSize: 447x0, 2: 447x0, containerRectVertical: 447x0

Want:

winSize: 446x192, horizSectonSize: 447x96, 2: 447x96, containerRectVertical: 447x96

or

winSize: 446x192, horizSectonSize: 447x192, 2: 447x0, containerRectVertical: 447x192

Solution

  • Use

    GUILayout.FlexibleSpace();
    

    For example with horizontal layout with following code

    GUILayout.BeginHorizontal();
    GUILayout.Label("left");
    GUILayout.FlexibleSpace();
    GUILayout.Label("right");
    GUILayout.EndHorizontal();
    

    you get this:

    enter image description here

    Same thing works for vertical groups.

    Also see more detailed options here: https://docs.unity3d.com/ScriptReference/GUILayoutOption.html

    Available hints are:

    GUILayout.Width, GUILayout.Height, GUILayout.MinWidth, GUILayout.MaxWidth, GUILayout.MinHeight, GUILayout.MaxHeight, GUILayout.ExpandWidth, GUILayout.ExpandHeight.

    You can create your GUILayoutOption[] options once (in OnEnable) and re-use it with each editor repaint to gain some performance