55using System . Reflection ;
66using System . Linq ;
77using UnityObject = UnityEngine . Object ;
8- using System . Runtime . CompilerServices ;
98
109namespace UInspectorPlus {
11- internal struct TypedDrawer {
12- public Type drawerType ;
13- public Type targetType ;
14- public int priority ;
10+ internal struct TypedDrawer : IEquatable < TypedDrawer > {
11+ public readonly Type drawerType ;
12+ public readonly Type targetType ;
13+ public readonly int priority ;
14+
15+ public TypedDrawer ( Type drawerType , Type targetType , int priority ) {
16+ this . drawerType = drawerType ;
17+ this . targetType = targetType ;
18+ this . priority = priority ;
19+ }
20+
21+ public override int GetHashCode ( ) {
22+ return drawerType . GetHashCode ( ) ^ targetType . GetHashCode ( ) ;
23+ }
24+
25+ public override bool Equals ( object obj ) => obj is TypedDrawer other && Equals ( other ) ;
26+
27+ public bool Equals ( TypedDrawer other ) =>
28+ drawerType . Equals ( other . drawerType ) &&
29+ targetType . Equals ( other . targetType ) ;
30+ }
31+
32+ [ AttributeUsage ( AttributeTargets . Class ) ]
33+ public class CustomInspectorDrawerAttribute : Attribute {
34+ public Type TargetType { get ; private set ; }
35+ public int Priority { get ; private set ; }
36+
37+ public CustomInspectorDrawerAttribute ( Type targetType , int priority = 0 ) {
38+ TargetType = targetType ;
39+ Priority = priority ;
40+ }
41+
42+ internal TypedDrawer ToDrawer ( Type drawerType ) => new TypedDrawer ( drawerType , TargetType , Priority ) ;
43+
44+ internal static CustomInspectorDrawerAttribute [ ] GetAttributes ( Type type ) =>
45+ GetCustomAttributes ( type , typeof ( CustomInspectorDrawerAttribute ) ) as CustomInspectorDrawerAttribute [ ] ??
46+ new CustomInspectorDrawerAttribute [ 0 ] ;
1547 }
1648
1749 public class InspectorDrawer : IDisposable {
18- private static readonly List < TypedDrawer > typedDrawers = new List < TypedDrawer > ( ) ;
50+ private static readonly HashSet < TypedDrawer > typedDrawers = new HashSet < TypedDrawer > ( ) ;
1951 public object target ;
2052 internal readonly List < IReflectorDrawer > drawer = new List < IReflectorDrawer > ( ) ;
2153 private readonly HashSet < IReflectorDrawer > removingDrawers = new HashSet < IReflectorDrawer > ( ) ;
2254 public bool shown ;
23- public bool isInternalType ;
2455 public bool changed ;
2556 public string searchText ;
2657 public event Action OnRequireRedraw ;
2758 protected Type targetType ;
2859 protected bool allowPrivate ;
2960 protected readonly bool allowMethods ;
3061
31- public static void RegisterCustomInspectorDrawer < T > ( Type targetType , int priority = 0 ) where T : InspectorDrawer {
32- typedDrawers . Add ( new TypedDrawer {
33- targetType = targetType ,
34- drawerType = typeof ( T ) ,
35- priority = priority
36- } ) ;
62+ static InspectorDrawer ( ) {
63+ var currentDomain = AppDomain . CurrentDomain ;
64+ currentDomain . AssemblyLoad += OnAssemblyLoad ;
65+ foreach ( var assembly in currentDomain . GetAssemblies ( ) )
66+ RegisterInspectorDrawers ( assembly ) ;
3767 }
3868
69+ private static void OnAssemblyLoad ( object sender , AssemblyLoadEventArgs args ) => RegisterInspectorDrawers ( args . LoadedAssembly ) ;
70+
71+ private static void RegisterInspectorDrawers ( Assembly assembly ) {
72+ foreach ( var type in assembly . GetTypes ( ) )
73+ if ( type . IsClass && ! type . IsAbstract && type . IsSubclassOf ( typeof ( InspectorDrawer ) ) )
74+ typedDrawers . UnionWith ( CustomInspectorDrawerAttribute . GetAttributes ( type ) . Select ( drawer => drawer . ToDrawer ( type ) ) ) ;
75+ }
76+
77+
3978 public static InspectorDrawer GetDrawer ( object target , Type targetType , bool shown , bool showProps , bool showPrivateFields , bool showObsolete , bool showMethods ) {
40- foreach ( var type in AppDomain . CurrentDomain . GetAssemblies ( )
41- . SelectMany ( asm => asm . GetTypes ( ) )
42- . Where ( type => type . IsClass && ! type . IsAbstract && type . IsSubclassOf ( typeof ( InspectorDrawer ) ) ) )
43- RuntimeHelpers . RunClassConstructor ( type . TypeHandle ) ;
4479 int lastPriority = int . MinValue ;
4580 Type drawerType = null ;
4681 try {
@@ -66,7 +101,6 @@ public InspectorDrawer(object target, Type targetType, bool shown, bool showProp
66101 this . targetType = targetType ;
67102 var fields = targetType . GetFields ( flag ) ;
68103 var props = ! showProps ? null : targetType . GetProperties ( flag ) . Where ( prop => prop . GetIndexParameters ( ) . Length == 0 ) . ToArray ( ) ;
69- isInternalType = ! targetType . IsSubclassOf ( typeof ( MonoBehaviour ) ) || Attribute . IsDefined ( targetType , typeof ( ExecuteInEditMode ) ) ;
70104 foreach ( var field in fields )
71105 try {
72106 if ( ! showObsolete && Attribute . IsDefined ( field , typeof ( ObsoleteAttribute ) ) )
@@ -91,6 +125,7 @@ public InspectorDrawer(object target, Type targetType, bool shown, bool showProp
91125 continue ;
92126 if ( ! showObsolete && Attribute . IsDefined ( prop , typeof ( ObsoleteAttribute ) ) )
93127 continue ;
128+ bool isInternalType = Helper . IsInternalType ( prop . DeclaringType ) ;
94129 drawer . Add ( new MethodPropertyDrawer ( prop , target , showPrivateFields , showObsolete , prop . CanRead && EditorApplication . isPlaying ) {
95130 AllowReferenceMode = false ,
96131 Updatable = isInternalType || Helper . GetState ( prop , false ) ,
@@ -177,6 +212,7 @@ private void DrawRequestRefs() {
177212 if ( drawer . requiredType . IsAssignableFrom ( targetType ) &&
178213 GUILayout . Button ( $ "Assign this object to { drawer . name } ") ) {
179214 drawer . Value = target ;
215+ drawer . SetDirty ( ) ;
180216 removal = drawer ;
181217 }
182218 if ( removal != null ) MethodPropertyDrawer . drawerRequestingReferences . Remove ( removal ) ;
@@ -189,7 +225,7 @@ public virtual void UpdateValues(bool updateProps) {
189225 if ( propDrawer == null )
190226 continue ;
191227 var isPropInfo = propDrawer . Info is PropertyInfo ;
192- if ( ! isInternalType && ( ! updateProps || ! propDrawer . Updatable ) && isPropInfo )
228+ if ( ! Helper . IsInternalType ( propDrawer . Info . DeclaringType ) && ( ! updateProps || ! propDrawer . Updatable ) && isPropInfo )
193229 continue ;
194230 propDrawer . UpdateValue ( ) ;
195231 }
0 commit comments