From 70f2cdaf16347010d6b7b7aa82ceddb45c105ed6 Mon Sep 17 00:00:00 2001 From: Johannes Deml Date: Wed, 17 Jan 2024 13:27:49 +0100 Subject: [PATCH] Add logic to check for inherited PropertyDrawers from base classes and interfaces This way an inherited drawer will be used if it exists, which might not be too uncommon in the case of SerializeReference fields --- .../Editor/PropertyDrawerCache.cs | 36 +++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/PropertyDrawerCache.cs b/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/PropertyDrawerCache.cs index 9b3f6df..415dd3b 100644 --- a/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/PropertyDrawerCache.cs +++ b/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/PropertyDrawerCache.cs @@ -25,6 +25,8 @@ public static bool TryGetPropertyDrawer (Type type,out PropertyDrawer drawer) static Type GetCustomPropertyDrawerType (Type type) { + Type[] interfaceTypes = type.GetInterfaces(); + foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()) { foreach (Type drawerType in assembly.GetTypes()) @@ -36,9 +38,39 @@ static Type GetCustomPropertyDrawerType (Type type) if (field != null) { var fieldType = field.GetValue(customPropertyDrawer) as Type; - if (fieldType != null && fieldType == type) + if (fieldType != null) { - return drawerType; + if (fieldType == type) + { + return drawerType; + } + + // If the property drawer also allows for being applied to child classes, check if they match + var useForChildrenField = customPropertyDrawer.GetType().GetField("m_UseForChildren", BindingFlags.NonPublic | BindingFlags.Instance); + if (useForChildrenField != null) + { + object useForChildrenValue = useForChildrenField.GetValue(customPropertyDrawer); + if (useForChildrenValue is bool && (bool)useForChildrenValue) + { + // Check interfaces + if (Array.Exists(interfaceTypes, interfaceType => interfaceType == fieldType)) + { + return drawerType; + } + + // Check derived types + Type baseType = type.BaseType; + while (baseType != null) + { + if (baseType == fieldType) + { + return drawerType; + } + + baseType = baseType.BaseType; + } + } + } } } }