Home > Blockchain >  How to align fields to the center of the line in editor window?
How to align fields to the center of the line in editor window?

Time:05-18

EditorGUILayout.BeginHorizontal();
allChildren[i] = EditorGUILayout.ObjectField(allChildren[i], typeof(Transform), true) as Transform;
EditorGUILayout.LabelField(names[i]);
EditorGUILayout.EndHorizontal();

Using the Begin/End Horizontal make the lables to be on the right side of the objectfield.

Labels on the right size

But now i want each objectfield and the lable near it both to be align to the center. So each objectfield and lablefield will be in the center of the line they are in.

Now they are all align to the left side.

CodePudding user response:

You can use FlexibleSpace between the elements to fill the space not taken by the elements.

EditorGUILayout.BeginHorizontal();
{
    GUILayout.FlexibleSpace(); // Fill Space Beginning

    allChildren[i] = EditorGUILayout.ObjectField(allChildren[i], typeof(Transform), true) as Transform;

    GUILayout.FlexibleSpace(); // Fill Space Middle

    GUILayout.Label(names[i]);

    GUILayout.FlexibleSpace(); // Fill Space End
}
EditorGUILayout.EndHorizontal();

It does not seem to work with EditorGUILayout.LabelField, replaced above using GUILayout.Label.

If you want the label on the right half and the object field on the left half (perfectly even), then you would need to set the explicit size of each (both being equal) in order for the centering to work correctly.

All of the elements you are using have params GUILayoutOption[] which is how you specify the width of the element. eg.

GUILayout.Label(names[i], GUILayout.Width(150));
  • Related