dbrizov / NaughtyAttributes

Attribute Extensions for Unity
MIT License
4.47k stars 463 forks source link

[AllowNesting] won't find CustomPropertyDrawers... and how I fixed it. #357

Open Samhayne opened 1 year ago

Samhayne commented 1 year ago

So with [AllowNesting] a CustomPropertyDrawer of nested classes seems to be ignored.

I fixed it by:

a) Downloading the script from here that would lookup the correct drawer: https://forum.unity.com/threads/solved-custompropertydrawer-not-being-using-in-editorgui-propertyfield.534968/ https://gist.github.com/wappenull/2391b3c23dd20ede74483d0da4cab3f1

b) Fixed a reflection issue with serialized private fields in that PropertyDrawerFinder.cs script in around line 120 by adding binding flags:

[...]
else  
{
     fi = resolvedType.GetField( fullPath[i], BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.DeclaredOnly );
     resolvedType = fi.FieldType;
}
[...]

c) Edited the AllowNestingPropertyDrawer.cs to this:

    public class AllowNestingPropertyDrawer : PropertyDrawerBase
    {
        protected override void OnGUI_Internal(Rect rect, SerializedProperty property, GUIContent label)
        {
            EditorGUI.BeginProperty(rect, label, property);
            //EditorGUI.PropertyField(rect, property, label, true);
            PropertyDrawer propertyDrawer = PropertyDrawerFinder.FindDrawerForProperty(property);
            if (propertyDrawer == null)
            {
                EditorGUI.PropertyField(rect, property, label, true);
            }
            else
            {
                propertyDrawer.OnGUI(rect, property, label);
            }
            EditorGUI.EndProperty();
        }

        protected override float GetPropertyHeight_Internal(SerializedProperty property, GUIContent label)
        {
            PropertyDrawer cust = PropertyDrawerFinder.FindDrawerForProperty(property);
            if (cust != null)
            {
                return cust.GetPropertyHeight(property, label);
            }
            else
            {
                return base.GetPropertyHeight_Internal(property, label);
            }
        }
    }

I don't feel competent enough in editor scripting to judge if this is a solid fix but I still wanted to share it.

Before: image

After: image

adamgryu commented 1 year ago

I'm running into this issue too. If there's a fork out there with this fix, I'd use it!