- Home /
Remove space between fields in BeginHorizontal
So I have an issue with EditorGUILayout.BeginHorizontal and fields within it.
No matter what I use the labelfields within a horizontal field will space themselves out across a horizontal area rather than snap right next to each other. You can resize the window and see the fields actively move to add/remove the space on their own to fill out the space equally. I've tried using ExpandWidth and labelWidth but nothing changes with their usage. 
This is my code:
private GUIStyle AreaStyleNoMargin {
get {
GUIStyle s = new GUIStyle(EditorStyles.textArea) {
margin = new RectOffset(0, 0, 0, 0),
};
return s;
}
}
private GUIStyle AreaStyleNoPadding {
get {
GUIStyle s = new GUIStyle(EditorStyles.textArea) {
padding = new RectOffset(0, 0, 0, 0)
};
return s;
}
}
private GUIStyle AreaStyleNoMarginNoPadding {
get {
GUIStyle s = new GUIStyle(EditorStyles.textArea) {
margin = new RectOffset(0, 0, 0, 0),
padding = new RectOffset(0, 0, 0, 0),
};
return s;
}
}
//....
private void DrawDatabaseEntries() {
CheckInit();
EditorGUILayout.BeginVertical();
{
EditorGUILayout.BeginHorizontal();
{
Rect DBList = EditorGUILayout.BeginVertical(AreaStyleNoPadding, GUILayout.MaxWidth(((position.width / 4) * 3) + 30), GUILayout.MaxHeight((position.height / 4) * 3), GUILayout.MinHeight((position.height / 4) * 3));
{
dbEntriesScroll = GUILayout.BeginScrollView(dbEntriesScroll);
{
GUILayout.Box("", EditorStyles.toolbar, GUILayout.ExpandWidth(true));
GUILayout.Space((EditorGUIUtility.singleLineHeight * -1f) - 2.5f);
//Sorter Tabs
GUILayout.BeginHorizontal();
{
if (GUILayout.Button("ID", TabStyleNoMargin)) {
//DBSort = ManagerDBSort.LoadOrder;
//SortEditorDB();
}
if (GUILayout.Button("Name", TabStyleNoMargin)) {
//DBSort = ManagerDBSort.Name;
//SortEditorDB();
}
}
GUILayout.EndHorizontal();
if (selectedDatabase != null) {
foreach (DatabaseEntry dbE in selectedDatabase.entries) {
EditorGUILayout.BeginHorizontal(AreaStyleNoMarginNoPadding);
EditorGUIUtility.labelWidth = 1;
EditorGUILayout.LabelField(dbE.ID, AreaStyleNoMargin, GUILayout.MaxWidth(60), GUILayout.MinWidth(60), GUILayout.MinHeight(EditorGUIUtility.singleLineHeight + 2), GUILayout.ExpandWidth(false));
EditorGUILayout.LabelField(dbE.Name, AreaStyleNoMargin, GUILayout.MaxWidth(60), GUILayout.MinWidth(60), GUILayout.MinHeight(EditorGUIUtility.singleLineHeight + 2), GUILayout.ExpandWidth(false));
EditorGUIUtility.labelWidth = 0;
EditorGUILayout.EndHorizontal();
}
}
//Draw Lines
Handles.BeginGUI();
Handles.color = Color.gray;
for (int i = 0; i < 7; i++) {
Handles.DrawLine(new Vector2(DBList.position.x + (100 * (i + 1)) - 2, DBList.position.y - 20), new Vector2(DBList.position.x + (100 * (i + 1)) - 2, DBList.position.y + DBList.size.y));
}
Handles.EndGUI();
}
GUILayout.EndScrollView();
}
EditorGUILayout.EndVertical();
}
EditorGUILayout.EndHorizontal();
}
EditorGUILayout.EndVertical();
}
Answer by Bunny83 · Jul 24, 2020 at 11:15 AM
The layout system will distribute all left over space to all flexible spaces. If you want to collect all the left over space at one point, use GUILayout.FlexibleSpace(). So place this as the last element in your layout group. you may want to have a look at my IMGUI crash course. I have a mirror version on github since UnityAnswers has suddenly messed up the markup formatting.
ps: you shouldn't use a LabelField. A LabelField is meant to provide a label for some other control or for a simple text field. Have a look at the source code. If you just want to display data, just use a GUILayout.Label.
Though keep in mind that Unity does not have a "table" layout. Each layout group is independent from other groups. To me it seems you try to build a table. Two or more seperate horizontal layout groups won't align each other unless their content has the exact same size. It's better to provide a fix column size for each element. Keep in mind that you can use "GUILayout.Width(yourWidth)" as a GUILayoutOption to pretty much any layouted control.
Answer by KingsHere · Jul 24, 2020 at 08:29 AM
Updating the answer after trying out @Bunny83 's comment and answer in Unity Editor:
For me this helped :
GUILayout.FlexibleSpace();
EditorGUILayout.EndHorizontal();
Putting the GUILayout.FlexibleSpace(); before EditorGUILayout.EndHorizontal(); is the right way. All the spacing is managed automagically.

Answer by Daniellsy · Jul 25, 2020 at 02:09 PM
private GUIStyle AreaStyleNoMargin { get { GUIStyle s = new GUIStyle(EditorStyles. textArea) { margin = new RectOffset(0, 0, 0, 0), }; return s; } }
Answer by Monkeydl_1929 · Mar 19, 2019 at 03:37 AM
@NeonTheCoder I found the answer to this question here:
https://forum.unity.com/threads/much-too-large-space-between-label-and-field-how-to-fix.312963/
Your answer
Follow this Question
Related Questions
Dynamic Editor Window Layout 0 Answers
Custom editor window stopped showing up 1 Answer
Small GUILayout button 1 Answer
Change script from EditorWindow 0 Answers
Populating object fields in a loop - editor scripting 1 Answer