Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Samhayne opened this issue May 25, 2023 · 1 comment
Open

Comments

@Samhayne
Copy link

Samhayne commented May 25, 2023

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants