diff --git a/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/PropertyDrawerCache.cs b/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/PropertyDrawerCache.cs new file mode 100644 index 0000000..9b3f6df --- /dev/null +++ b/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/PropertyDrawerCache.cs @@ -0,0 +1,52 @@ +#if UNITY_2019_3_OR_NEWER +using System; +using System.Collections.Generic; +using UnityEditor; +using System.Reflection; + +namespace MackySoft.SerializeReferenceExtensions.Editor +{ + public static class PropertyDrawerCache + { + + static readonly Dictionary s_Caches = new Dictionary(); + + public static bool TryGetPropertyDrawer (Type type,out PropertyDrawer drawer) + { + if (!s_Caches.TryGetValue(type,out drawer)) + { + Type drawerType = GetCustomPropertyDrawerType(type); + drawer = (drawerType != null) ? (PropertyDrawer)Activator.CreateInstance(drawerType) : null; + + s_Caches.Add(type, drawer); + } + return (drawer != null); + } + + static Type GetCustomPropertyDrawerType (Type type) + { + foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()) + { + foreach (Type drawerType in assembly.GetTypes()) + { + var customPropertyDrawerAttributes = drawerType.GetCustomAttributes(typeof(CustomPropertyDrawer), true); + foreach (CustomPropertyDrawer customPropertyDrawer in customPropertyDrawerAttributes) + { + var field = customPropertyDrawer.GetType().GetField("m_Type", BindingFlags.NonPublic | BindingFlags.Instance); + if (field != null) + { + var fieldType = field.GetValue(customPropertyDrawer) as Type; + if (fieldType != null && fieldType == type) + { + return drawerType; + } + } + } + } + } + return null; + } + + } +} +#endif \ No newline at end of file diff --git a/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/PropertyDrawerCache.cs.meta b/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/PropertyDrawerCache.cs.meta new file mode 100644 index 0000000..0910a43 --- /dev/null +++ b/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/PropertyDrawerCache.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 075fe3cc9403a8947bc325d8515e9a69 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: