Skip to content

Commit e5a5157

Browse files
authored
Merge pull request #48 from JohannesDeml/bugfix/propertydrawer-allow-inheritance
Add logic to check for inherited PropertyDrawers from base classes and interfaces
2 parents 69830f3 + 70f2cda commit e5a5157

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/PropertyDrawerCache.cs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public static bool TryGetPropertyDrawer (Type type,out PropertyDrawer drawer)
2525

2626
static Type GetCustomPropertyDrawerType (Type type)
2727
{
28+
Type[] interfaceTypes = type.GetInterfaces();
29+
2830
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
2931
{
3032
foreach (Type drawerType in assembly.GetTypes())
@@ -36,9 +38,39 @@ static Type GetCustomPropertyDrawerType (Type type)
3638
if (field != null)
3739
{
3840
var fieldType = field.GetValue(customPropertyDrawer) as Type;
39-
if (fieldType != null && fieldType == type)
41+
if (fieldType != null)
4042
{
41-
return drawerType;
43+
if (fieldType == type)
44+
{
45+
return drawerType;
46+
}
47+
48+
// If the property drawer also allows for being applied to child classes, check if they match
49+
var useForChildrenField = customPropertyDrawer.GetType().GetField("m_UseForChildren", BindingFlags.NonPublic | BindingFlags.Instance);
50+
if (useForChildrenField != null)
51+
{
52+
object useForChildrenValue = useForChildrenField.GetValue(customPropertyDrawer);
53+
if (useForChildrenValue is bool && (bool)useForChildrenValue)
54+
{
55+
// Check interfaces
56+
if (Array.Exists(interfaceTypes, interfaceType => interfaceType == fieldType))
57+
{
58+
return drawerType;
59+
}
60+
61+
// Check derived types
62+
Type baseType = type.BaseType;
63+
while (baseType != null)
64+
{
65+
if (baseType == fieldType)
66+
{
67+
return drawerType;
68+
}
69+
70+
baseType = baseType.BaseType;
71+
}
72+
}
73+
}
4274
}
4375
}
4476
}

0 commit comments

Comments
 (0)