Skip to content

Commit 2b40c3d

Browse files
authored
Merge pull request #56 from theo-rapidfire/fix/use-type-cache
Performance improvement: Use TypeCache instead of manual type iteration
2 parents 301cb4b + f7c1017 commit 2b40c3d

File tree

1 file changed

+30
-32
lines changed

1 file changed

+30
-32
lines changed

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

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -26,49 +26,47 @@ public static bool TryGetPropertyDrawer (Type type,out PropertyDrawer drawer)
2626
static Type GetCustomPropertyDrawerType (Type type)
2727
{
2828
Type[] interfaceTypes = type.GetInterfaces();
29-
30-
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
29+
30+
var types = TypeCache.GetTypesWithAttribute<CustomPropertyDrawer>();
31+
foreach (Type drawerType in types)
3132
{
32-
foreach (Type drawerType in assembly.GetTypes())
33+
var customPropertyDrawerAttributes = drawerType.GetCustomAttributes(typeof(CustomPropertyDrawer), true);
34+
foreach (CustomPropertyDrawer customPropertyDrawer in customPropertyDrawerAttributes)
3335
{
34-
var customPropertyDrawerAttributes = drawerType.GetCustomAttributes(typeof(CustomPropertyDrawer), true);
35-
foreach (CustomPropertyDrawer customPropertyDrawer in customPropertyDrawerAttributes)
36+
var field = customPropertyDrawer.GetType().GetField("m_Type", BindingFlags.NonPublic | BindingFlags.Instance);
37+
if (field != null)
3638
{
37-
var field = customPropertyDrawer.GetType().GetField("m_Type", BindingFlags.NonPublic | BindingFlags.Instance);
38-
if (field != null)
39+
var fieldType = field.GetValue(customPropertyDrawer) as Type;
40+
if (fieldType != null)
3941
{
40-
var fieldType = field.GetValue(customPropertyDrawer) as Type;
41-
if (fieldType != null)
42+
if (fieldType == type)
4243
{
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)
44+
return drawerType;
45+
}
46+
47+
// If the property drawer also allows for being applied to child classes, check if they match
48+
var useForChildrenField = customPropertyDrawer.GetType().GetField("m_UseForChildren", BindingFlags.NonPublic | BindingFlags.Instance);
49+
if (useForChildrenField != null)
50+
{
51+
object useForChildrenValue = useForChildrenField.GetValue(customPropertyDrawer);
52+
if (useForChildrenValue is bool && (bool)useForChildrenValue)
5153
{
52-
object useForChildrenValue = useForChildrenField.GetValue(customPropertyDrawer);
53-
if (useForChildrenValue is bool && (bool)useForChildrenValue)
54+
// Check interfaces
55+
if (Array.Exists(interfaceTypes, interfaceType => interfaceType == fieldType))
5456
{
55-
// Check interfaces
56-
if (Array.Exists(interfaceTypes, interfaceType => interfaceType == fieldType))
57+
return drawerType;
58+
}
59+
60+
// Check derived types
61+
Type baseType = type.BaseType;
62+
while (baseType != null)
63+
{
64+
if (baseType == fieldType)
5765
{
5866
return drawerType;
5967
}
6068

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-
}
69+
baseType = baseType.BaseType;
7270
}
7371
}
7472
}

0 commit comments

Comments
 (0)