Max Play
Max Play

Reputation: 4037

EditorGUILayout.BeginHorizontal() returns an empty Rect

So I wanted to format a custom EditorWindow I am working on and I organized everything in Horizontal Layouts so the elements fit nicely like table rows.

Unfortunately, my header-row was off, so I tried to unify everything by getting the base Rect from the EditorGUILayout.BeginHorizontal() method.

Unfortunately, it returns a Rect with default values (everything is 0). So I can't work with it properly. Am I missing something or why does it return an empty Rect? In the EditorWindow itself, space is filled.

Example Code:

Rect boundary = EditorGUILayout.BeginHorizontal();
float largeWidth = boundary.width * 0.4f;
float smallWidth = boundary.width * 0.2f;
EditorGUILayout.LabelField("stuff1", GUILayout.Width(largeWidth));
EditorGUILayout.LabelField("stuff2", GUILayout.Width(largeWidth));
EditorGUILayout.LabelField("stuff3", GUILayout.Width(smallwidth));
EditorGUILayout.EndHorizontal();

Upvotes: 0

Views: 1489

Answers (1)

Max Play
Max Play

Reputation: 4037

Okay, so I found a blog article online, stating the obvious:

The Rect won't be filled, when the Object does not no how large it will be. The Information will be available when an event is fired that does not consist of informationgathering.

So what I ended up doing was:

  • I saved the window width (which is available at all time) in a field at the beginning of OnGUI()
  • I created some constants to mess with so everything can be aligned
  • These constants were just assumptions about the spacings between elements and the padding to the left and right (I know these informations are available, but it did fit, so I didn't care)

After all this is a dirty solution, but it did work.

Here is some example code for those who are interested:

public class CustomWindow : EditorWindow
{
    #region Private Fields

    private Vector2 scrollLocation;
    private float elementsWidth;
    private float textBoxWidth;
    private float buttonWidth;
    private const int WINDOW_MIN_SIZE = 400;
    private const int BORDER_SPACING = 10;
    private const int ELEMENT_SPACING = 8;

    #endregion Private Fields

    #region Public Methods

    [MenuItem("Window/CustomWindow")]
    public static void ShowWindow()
    {
        CustomWindow window = GetWindow(typeof(CustomWindow), false, "CustomWindow") as CustomWindow;
        window.Show();
        window.minSize = new Vector2(WINDOW_MIN_SIZE, WINDOW_MIN_SIZE);
        window.Load();
    }

    #endregion Public Methods

    #region Private Methods

    private void OnGUI()
    {
        elementsWidth = EditorGUIUtility.currentViewWidth - BORDER_SPACING * 2;
        textBoxWidth = elementsWidth * 0.4f;
        buttonWidth = elementsWidth * 0.2f;
        scrollLocation = EditorGUILayout.BeginScrollView(scrollLocation);
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("stuff1", GUILayout.Width(largeWidth));
        EditorGUILayout.LabelField("stuff2", GUILayout.Width(largeWidth));
        EditorGUILayout.LabelField("stuff3", GUILayout.Width(smallwidth));
        EditorGUILayout.EndHorizontal();
        EditorGUILayout.EndScrollView();
    }

    #endregion Private Methods
}

Upvotes: 1

Related Questions