Search Unity

How do I align the label text in a property drawer?

Discussion in 'Immediate Mode GUI (IMGUI)' started by AcademyOfFetishes, Mar 24, 2019.

  1. AcademyOfFetishes

    AcademyOfFetishes

    Joined:
    Nov 16, 2018
    Posts:
    219
    I have a custom drawer that's putting all these bools on one row. But it's kind of hard to tell which label is associated with which checkbox. I think aligning the label's right would help, but I'm not sure how to do that:


    Here's my current code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEditor;
    4. using UnityEngine;
    5.  
    6. [CustomPropertyDrawer(typeof(CharacterAccessory))]
    7. public class CharacterAccessoryDrawer : PropertyDrawer
    8. {
    9.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    10.     {
    11.         EditorGUI.BeginProperty(position, label, property);
    12.  
    13.         var indent = EditorGUI.indentLevel;
    14.         EditorGUI.indentLevel = 1;
    15.  
    16.         SerializedProperty nameProperty = property.FindPropertyRelative("name");
    17.         SerializedProperty spritesProperty = property.FindPropertyRelative("sprites");
    18.         SerializedProperty unlockableProperty = property.FindPropertyRelative("unlockable");
    19.         SerializedProperty jacketProperty = property.FindPropertyRelative("jacket");
    20.         SerializedProperty topProperty = property.FindPropertyRelative("top");
    21.         SerializedProperty bottomProperty = property.FindPropertyRelative("bottom");
    22.         SerializedProperty ankleProperty = property.FindPropertyRelative("ankle");
    23.         SerializedProperty neckProperty = property.FindPropertyRelative("neck");
    24.  
    25.         var yPosition = position.y;
    26.         var nameRect = new Rect(position.x, yPosition, 175, EditorGUI.GetPropertyHeight(nameProperty));
    27.         var spritesRect = new Rect(position.x + 180, yPosition, position.width - 155, EditorGUI.GetPropertyHeight(spritesProperty));
    28.         yPosition += EditorGUI.GetPropertyHeight(nameProperty) + EditorGUI.GetPropertyHeight(spritesProperty);
    29.         var unlockableRect = new Rect(position.x, yPosition, position.width, EditorGUI.GetPropertyHeight(unlockableProperty));
    30.         yPosition += EditorGUI.GetPropertyHeight(unlockableProperty);
    31.         var xPosition = position.x;
    32.         var neckRect = new Rect(xPosition, yPosition, 90, EditorGUI.GetPropertyHeight(neckProperty));
    33.         xPosition += 80;
    34.         var jacketRect = new Rect(xPosition, yPosition, 90, EditorGUI.GetPropertyHeight(jacketProperty));
    35.         xPosition += 80;
    36.         var topRect = new Rect(xPosition, yPosition, 90, EditorGUI.GetPropertyHeight(topProperty));
    37.         xPosition += 80;
    38.         var bottomRect = new Rect(xPosition, yPosition, 90, EditorGUI.GetPropertyHeight(bottomProperty));
    39.         xPosition += 80;
    40.         var ankleRect = new Rect(xPosition, yPosition, 90, EditorGUI.GetPropertyHeight(ankleProperty));
    41.  
    42.         EditorGUI.PropertyField(nameRect, nameProperty, GUIContent.none);
    43.         EditorGUI.PropertyField(spritesRect, spritesProperty, true);
    44.         EditorGUI.PropertyField(unlockableRect, unlockableProperty, true);
    45.         var labelWidth = EditorGUIUtility.labelWidth;      
    46.         EditorGUIUtility.labelWidth = 70;
    47.         EditorGUI.PropertyField(neckRect, neckProperty);
    48.         EditorGUI.PropertyField(jacketRect, jacketProperty);
    49.         EditorGUI.PropertyField(topRect, topProperty);
    50.         EditorGUI.PropertyField(bottomRect, bottomProperty);
    51.         EditorGUI.PropertyField(ankleRect, ankleProperty);
    52.         EditorGUIUtility.labelWidth = labelWidth;
    53.  
    54.         EditorGUI.indentLevel = indent;
    55.  
    56.         EditorGUI.EndProperty();
    57.     }
    58.  
    59.     public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    60.     {
    61.         float totalHeight = 0;
    62.         totalHeight += EditorGUI.GetPropertyHeight(property.FindPropertyRelative("sprites"));
    63.         totalHeight += EditorGUI.GetPropertyHeight(property.FindPropertyRelative("top"));
    64.         totalHeight += EditorGUI.GetPropertyHeight(property.FindPropertyRelative("unlockable"));
    65.         totalHeight += 30;
    66.         return totalHeight;
    67.     }
    68.  
    69. }
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,281
    AcademyOfFetishes likes this.
  3. lrasomattos

    lrasomattos

    Joined:
    Aug 22, 2015
    Posts:
    29
    How do we set the style of the label in a property drawer when using BeginProperty and PropertyField?
    BeginProperty and PropertyField don't seem to have a way to change the style, and GUIContent doesn't contain the style itself.

    I tried setting GUI.skin.label directly (and resetting after drawing) but it has no effect.
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,281
    I don't think you can. You need to explicitly draw it with a style and type, PropertyField will handle the style and type with defaults.

    So you would need to do stuff like if the property is int then use Int field with style etc.
     
  5. lrasomattos

    lrasomattos

    Joined:
    Aug 22, 2015
    Posts:
    29
    Thanks for such a quick answer!

    Actually I just managed to do it through EditorStyles.label, instead of GUI.skin.label.
    While it doesn't have a setter, the getter seems to return the original instance of the default label, which can be altered.

    Just gotta be sure to cache the original values and resetting them after drawing.

    Code (CSharp):
    1.  
    2.         TextAnchor orginalAlignment = EditorStyles.label.alignment;
    3.         EditorStyles.label.alignment = TextAnchor.MiddleRight;
    4.        
    5.         label = EditorGUI.BeginProperty(propRect, new GUIContent(shortLabels ? "Val" : "Value"), valueProp);
    6.         EditorGUI.PropertyField(propRect, valueProp, hideLabels ? GUIContent.none : label);
    7.         EditorGUI.EndProperty();
    8.         }
    9.  
    10.         EditorStyles.label.alignment = orginalAlignment;
    11.  
     
  6. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,281
    Ah that should work. I should have thought of that ;)
     
    lrasomattos likes this.