timo-sisus / Power-Inspector

Report bugs and offer improvement ideas for Power Inspector.
http://u3d.as/1sNc
7 stars 0 forks source link

Custom PropertyDrawers & Editors targeting open generic types don't get used #21

Closed DarrenRuaneKaizen closed 4 years ago

DarrenRuaneKaizen commented 4 years ago

I've got a custom property drawer targeting an open generic type like the following: [CustomPropertyDrawer(typeof(ClampableVariable<>), true)]

I then have types deriving from this type like: class ClampableFloat : ClampableVariable<float> class ClampableChar : ClampableVariable<char>

I've also got a custom Editor like: [CustomEditor(typeof(ClampableVariable<>), true)]

PowerInspector does not detect that these types implement this generic type as a result of how Type.IsSubclassOf works and thus the PropertyDrawer and Editor does not get used to render these types in PowerInspector.

I found this StackOverflow post which provides a handy method for checking if a type inherits from a given generic type:

static bool IsSubclassOfRawGeneric(Type generic, Type toCheck) {
    while (toCheck != null && toCheck != typeof(object)) {
        var cur = toCheck.IsGenericType ? toCheck.GetGenericTypeDefinition() : toCheck;
        if (generic == cur) {
            return true;
        }
        toCheck = toCheck.BaseType;
    }
    return false;
}

I noticed that Unity's native inspector can handle rendering the Editor but not the PropertyDrawer (even though they both target the open generic type).

timo-sisus commented 4 years ago

Thanks for the report! Fix for this will be included in Update 1.3.4.