Skip to content

Commit 92c16ed

Browse files
committed
Add PropertyDrawerCache
1 parent 4ac262c commit 92c16ed

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#if UNITY_2019_3_OR_NEWER
2+
using System;
3+
using System.Collections.Generic;
4+
using UnityEditor;
5+
using System.Reflection;
6+
7+
namespace MackySoft.SerializeReferenceExtensions.Editor
8+
{
9+
public static class PropertyDrawerCache
10+
{
11+
12+
static readonly Dictionary<Type, PropertyDrawer> s_Caches = new Dictionary<Type, PropertyDrawer>();
13+
14+
public static bool TryGetPropertyDrawer (Type type,out PropertyDrawer drawer)
15+
{
16+
if (!s_Caches.TryGetValue(type,out drawer))
17+
{
18+
Type drawerType = GetCustomPropertyDrawerType(type);
19+
drawer = (drawerType != null) ? (PropertyDrawer)Activator.CreateInstance(drawerType) : null;
20+
21+
s_Caches.Add(type, drawer);
22+
}
23+
return (drawer != null);
24+
}
25+
26+
static Type GetCustomPropertyDrawerType (Type type)
27+
{
28+
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
29+
{
30+
foreach (Type drawerType in assembly.GetTypes())
31+
{
32+
var customPropertyDrawerAttributes = drawerType.GetCustomAttributes(typeof(CustomPropertyDrawer), true);
33+
foreach (CustomPropertyDrawer customPropertyDrawer in customPropertyDrawerAttributes)
34+
{
35+
var field = customPropertyDrawer.GetType().GetField("m_Type", BindingFlags.NonPublic | BindingFlags.Instance);
36+
if (field != null)
37+
{
38+
var fieldType = field.GetValue(customPropertyDrawer) as Type;
39+
if (fieldType != null && fieldType == type)
40+
{
41+
return drawerType;
42+
}
43+
}
44+
}
45+
}
46+
}
47+
return null;
48+
}
49+
50+
}
51+
}
52+
#endif

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

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)