Skip to content

Commit 9d3037b

Browse files
committed
Optimize type caching using the UnityEditor.TypeCache API
1 parent 52efae0 commit 9d3037b

1 file changed

Lines changed: 19 additions & 43 deletions

File tree

Assets/Editor Toolbox/Scripts/Attributes/RegularAttributes/TypeConstraintAttribute.cs

Lines changed: 19 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
4+
using System.Linq;
45
using System.Reflection;
56

6-
using Toolbox;
7+
using UnityEditor;
78

89
namespace UnityEngine
910
{
@@ -25,32 +26,31 @@ protected TypeConstraintAttribute(Type assemblyType)
2526
/// <summary>
2627
/// Get all proper types from executing assembly.
2728
/// </summary>
28-
public List<Type> GetFilteredTypes()
29+
public virtual List<Type> GetFilteredTypes()
2930
{
30-
var types = new List<Type>();
31-
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
32-
foreach (var assembly in assemblies)
31+
var types = TypeCache.GetTypesDerivedFrom(AssemblyType).ToList();
32+
for (var i = types.Count - 1; i >= 0; i--)
3333
{
34-
types.AddRange(GetFilteredTypes(assembly));
34+
var type = types[i];
35+
if (IsConstraintSatisfied(type))
36+
{
37+
continue;
38+
}
39+
40+
types.RemoveAt(i);
3541
}
3642

37-
types.Sort((a, b) => a.FullName.CompareTo(b.FullName));
3843
return types;
3944
}
4045

4146
/// <summary>
4247
/// Get all filtered type from provided assembly.
4348
/// </summary>
44-
public List<Type> GetFilteredTypes(Assembly assembly)
49+
public virtual List<Type> GetFilteredTypes(Assembly assembly)
4550
{
4651
var types = new List<Type>();
4752
foreach (var type in assembly.GetTypes())
4853
{
49-
if (!type.IsVisible || !type.IsClass)
50-
{
51-
continue;
52-
}
53-
5454
if (!IsConstraintSatisfied(type))
5555
{
5656
continue;
@@ -72,6 +72,12 @@ public List<Type> GetFilteredTypes(Assembly assembly)
7272
/// </returns>
7373
public virtual bool IsConstraintSatisfied(Type type)
7474
{
75+
//NOTE: it's possible to strip out ConstructedGenericTypes, but they are considered valid for now
76+
if (!type.IsVisible || !type.IsClass)
77+
{
78+
return false;
79+
}
80+
7581
return (AllowAbstract || !type.IsAbstract) && (AllowObsolete || !IsDefined(type, typeof(ObsoleteAttribute)));
7682
}
7783

@@ -134,19 +140,6 @@ public ClassExtendsAttribute() : base(typeof(object))
134140
/// <param name="baseType">Type of class that selectable classes must derive from.</param>
135141
public ClassExtendsAttribute(Type baseType) : base(baseType)
136142
{ }
137-
138-
139-
public override bool IsConstraintSatisfied(Type type)
140-
{
141-
if (type == AssemblyType || !base.IsConstraintSatisfied(type))
142-
{
143-
return false;
144-
}
145-
146-
return AssemblyType.IsGenericType
147-
? AssemblyType.IsAssignableFromGeneric(type)
148-
: AssemblyType.IsAssignableFrom(type);
149-
}
150143
}
151144

152145
///<inheritdoc/>
@@ -166,23 +159,6 @@ public ClassImplementsAttribute() : base(null)
166159
/// <param name="interfaceType">Type of interface that selectable classes must implement.</param>
167160
public ClassImplementsAttribute(Type interfaceType) : base(interfaceType)
168161
{ }
169-
170-
171-
public override bool IsConstraintSatisfied(Type type)
172-
{
173-
if (base.IsConstraintSatisfied(type))
174-
{
175-
foreach (var interfaceType in type.GetInterfaces())
176-
{
177-
if (interfaceType == AssemblyType)
178-
{
179-
return true;
180-
}
181-
}
182-
}
183-
184-
return false;
185-
}
186162
}
187163

188164
/// <summary>

0 commit comments

Comments
 (0)