Reputation: 4037
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
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:
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