Property Drawer and Children

Hi guys,

I’m trying to fake a dictionary in the inspector using an enum and an array. I got to the point where it looks (mostly) correct but when I override GetPropertyHeight it covers the foldout’s child and I can’t edit their values. Does anyone know a solution to this? Thanks in advance.

[15794-propertydrawer+bug.png|15794]ren

[CustomPropertyDrawer(typeof (NameListAttribute))]
public class NameListDrawer : PropertyDrawer
{
    private NameListAttribute nameListAttribute { get { return ((NameListAttribute) attribute); }}
    private bool foldout = false;
    private const float ITEMSIZE = 15f;


    public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    {
        if (foldout)
        {
            return base.GetPropertyHeight(property, label) + ITEMSIZE * Enum.GetNames(nameListAttribute.names).Length;
        }
        else
        {
            return base.GetPropertyHeight(property, label);
        }
    }

    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        foldout = EditorGUI.Foldout(position, foldout, label);
        if (foldout)
        {
            string[] names = Enum.GetNames(nameListAttribute.names);

            for (int i = 0; i < names.Length; i++)
            {
                Rect rect = EditorGUI.IndentedRect(position);
                rect.y += 15*(i + 1);
                rect.height = 15f;
                EditorGUI.PropertyField(rect, property.GetArrayElementAtIndex(i), new GUIContent(names*));*

}
}
}
}

When overriding GetPropertyHeight, it sets the height property of your Rect position to the total height. All you have to do is reset that height before calling EditorGUI.Foldout.

public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
    position.height = 16f;
    foldout = EditorGUI.Foldout(position, foldout, label);

Just a side note, instead of using the foldout variable used the built in property.isExpanded variable to see if you have expanded the property you are working with. PropertyDrawers are not 1-to-1 with the properties they draw.

you don’t call GetPropertyHeight after foldout, and names are set (from what I have found if you want something really special to happen in GetPropertyHeight then you need to call it yourself in your PropertyDrawer as repainting is not done by default, or as often like with EditorWindows, or CustomInspectors

the other suggestion is instead of doing hard set increments of rect.y (and rect.height; why is this being set to 15 in every iteration of the for loop?)

//just right before your forloop do:
rect.height = GetPropertyHeight/names.Length;
    //inside the for loop 
    rect.y += rect.height