11using System ;
22using System . Collections . Generic ;
33using System . Linq ;
4+ using System . Reflection ;
45
56using UnityEditor ;
67
@@ -42,20 +43,7 @@ public static TypesCachedCollection GetCollection(TypeConstraintContext constrai
4243 return new TypesCachedCollection ( ) ;
4344 }
4445
45- var typesCache = TypeCache . GetTypesDerivedFrom ( parentType ) ;
46- var typesList = typesCache . ToList ( ) ;
47- typesList . Add ( parentType ) ;
48- for ( var i = typesList . Count - 1 ; i >= 0 ; i -- )
49- {
50- var type = typesList [ i ] ;
51- if ( constraint . IsSatisfied ( type ) )
52- {
53- continue ;
54- }
55-
56- typesList . RemoveAt ( i ) ;
57- }
58-
46+ var typesList = FindTypes ( constraint ) ;
5947 return cachedCollections [ key ] = new TypesCachedCollection ( typesList ) ;
6048 }
6149
@@ -90,5 +78,51 @@ public static bool TryGetTypeFromManagedReferenceFullTypeName(string managedRefe
9078
9179 return false ;
9280 }
81+
82+ public static List < Type > FindTypes ( TypeConstraintContext constraint )
83+ {
84+ #if UNITY_2019_2_OR_NEWER
85+ var parentType = constraint . TargetType ;
86+ var typesCache = TypeCache . GetTypesDerivedFrom ( parentType ) ;
87+ var typesList = typesCache . ToList ( ) ;
88+ typesList . Add ( parentType ) ;
89+ for ( var i = typesList . Count - 1 ; i >= 0 ; i -- )
90+ {
91+ var type = typesList [ i ] ;
92+ if ( constraint . IsSatisfied ( type ) )
93+ {
94+ continue ;
95+ }
96+
97+ typesList . RemoveAt ( i ) ;
98+ }
99+ #else
100+ var typesList = new List < Type > ( ) ;
101+ var assemblies = AppDomain . CurrentDomain . GetAssemblies ( ) ;
102+ foreach ( var assembly in assemblies )
103+ {
104+ typesList . AddRange ( FindTypes ( constraint , assembly ) ) ;
105+ }
106+
107+ typesList . Sort ( ( a , b ) => a . FullName . CompareTo ( b . FullName ) ) ;
108+ #endif
109+ return typesList ;
110+ }
111+
112+ public static List < Type > FindTypes ( TypeConstraintContext constraint , Assembly assembly )
113+ {
114+ var types = new List < Type > ( ) ;
115+ foreach ( var type in assembly . GetTypes ( ) )
116+ {
117+ if ( ! constraint . IsSatisfied ( type ) )
118+ {
119+ continue ;
120+ }
121+
122+ types . Add ( type ) ;
123+ }
124+
125+ return types ;
126+ }
93127 }
94128}
0 commit comments