Search Unity

Question EditorGUI.PropertyField: Fields break when using labels

Discussion in 'Scripting' started by Bowbowis, Nov 25, 2020.

  1. Bowbowis

    Bowbowis

    Joined:
    Jun 7, 2017
    Posts:
    6
    I'm trying to set up a custom struct so it displays like a Vector2 in the editor. This works fine if I set the property fields to have no label by passing GUIContent.none, but if I try to use something else or don't pass an argument the field ends up broken in the inspector.

    To wit, the following code:
    Code (CSharp):
    1.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    2.     {
    3.         EditorGUI.BeginProperty(position, label, property);
    4.         position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
    5.         EditorGUI.PropertyField(new Rect(position.x, position.y, 30, position.height), property.FindPropertyRelative("min"), GUIContent.none);
    6.         EditorGUI.PropertyField(new Rect(position.x + 65, position.y, 30, position.height), property.FindPropertyRelative("max"), GUIContent.none);
    7.         EditorGUI.EndProperty();
    8.     }
    Results in this:
    UnlabeledField.png

    This:
    Code (CSharp):
    1.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    2.     {
    3.         EditorGUI.BeginProperty(position, label, property);
    4.         position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
    5.         EditorGUI.PropertyField(new Rect(position.x, position.y, 30, position.height), property.FindPropertyRelative("min"), new GUIContent ("Min"));
    6.         EditorGUI.PropertyField(new Rect(position.x + 65, position.y, 30, position.height), property.FindPropertyRelative("max"), new GUIContent ("Max"));
    7.         EditorGUI.EndProperty();
    8.     }
    Results in this:
    BrokenField.png

    And I want it to look like this:
    LabeledField.png
     
  2. dustincapps

    dustincapps

    Joined:
    Mar 9, 2018
    Posts:
    1
    Sorry to bump an old thread but I just ran into this issue in Unity 2022. The only fix I've been able to confirm thus far is to manually override EditorGUIUtility.labelWidth, and then restore it afterwards. In my case it was set to 363 by default, no idea if that is standard.

    Code (CSharp):
    1. GUIContent scaleStyle = new GUIContent("Scale: ");
    2. EditorGUIUtility.labelWidth = 100;
    3. EditorGUI.PropertyField(scaleRect, scale, scaleStyle);
    4. EditorGUIUtility.labelWidth = 363;
    Really gross, but it works.
     
  3. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    Labelwidth is calculated as a % of the Window-Width (/rect-width).
    So you'd really want to store it if you're changing it.

    You should always try to use %s of the total width, instead of hard-coded values.
    That way, your UI scales along with the window
     
    Bunny83 likes this.