简体   繁体   中英

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

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:

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM