Skip to content

Commit da79104

Browse files
committed
Minor fixes
1 parent 657486e commit da79104

6 files changed

Lines changed: 62 additions & 16 deletions

File tree

Assets/Editor Toolbox/Editor/Drawers/Regular/FormattedNumberAttributeDrawer.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ protected override void OnGUISafe(Rect position, SerializedProperty property, GU
5555
}
5656
else
5757
{
58+
#if UNITY_2019_2_OR_NEWER
5859
position.xMin += EditorGUIUtility.labelWidth + EditorGUIUtility.standardVerticalSpacing;
60+
#else
61+
position.xMin += EditorGUIUtility.labelWidth;
62+
#endif
5963
}
6064

6165
var targetAttribute = attribute as FormattedNumberAttribute;

Assets/Editor Toolbox/Editor/Internal/TypeConstraintContext.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ public TypeConstraintContext(Type targetType)
1616

1717
public virtual bool IsSatisfied(Type type)
1818
{
19+
#if UNITY_2019_2_OR_NEWER
1920
return type.IsVisible;
21+
#else
22+
return type.IsVisible && (targetType.IsGenericType
23+
? targetType.IsAssignableFromGeneric(type)
24+
: targetType.IsAssignableFrom(type));
25+
#endif
2026
}
2127

2228
public virtual void ApplyTarget(Type type)

Assets/Editor Toolbox/Editor/Internal/TypeConstraintStandard.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public override bool IsSatisfied(Type type)
2222
return base.IsSatisfied(type) &&
2323
//NOTE: consider moving allowAbstract && allowObsolete properties to the TypeSettings enum
2424
(!type.IsClass || Settings.HasFlag(TypeSettings.Class)) &&
25-
(!type.IsAbstract || Settings.HasFlag(TypeSettings.Interface) || AllowAbstract) &&
25+
(!type.IsAbstract || (Settings.HasFlag(TypeSettings.Interface) && type.IsInterface) || AllowAbstract) &&
2626
(!type.IsInterface || Settings.HasFlag(TypeSettings.Interface)) &&
2727
(!Attribute.IsDefined(type, typeof(ObsoleteAttribute)) || AllowObsolete);
2828
}

Assets/Editor Toolbox/Editor/Utilities/TypeUtilities.cs

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Reflection;
45

56
using 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
}

Assets/Editor Toolbox/Tests/Editor/TypesFilteringTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public void TestStandardConstraintPass5()
115115
[Test]
116116
public void TestStandardConstraintPass6()
117117
{
118-
var constraint = new TypeConstraintStandard(typeof(ClassBase), TypeSettings.Class | TypeSettings.Interface, true, false);
118+
var constraint = new TypeConstraintStandard(typeof(ClassBase), TypeSettings.Class | TypeSettings.Interface, false, false);
119119
var collection = TypeUtilities.GetCollection(constraint);
120120
Assert.IsFalse(collection.Contains(typeof(ClassBase)));
121121
Assert.IsTrue(collection.Contains(typeof(ClassWithInterface1)));

Assets/Examples/Scripts/SampleBehaviour6.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
[AddComponentMenu("Editor Toolbox/Cheat Sheet 6 (Serialize Reference)")]
77
public class SampleBehaviour6 : MonoBehaviour
88
{
9+
#if UNITY_2019_3_OR_NEWER
910
[SerializeReference, ReferencePicker]
1011
public Interface1 var1;
1112
[SerializeReference, ReferencePicker]
1213
public ClassWithInterfaceBase var2;
14+
#endif
1315

1416
public interface Interface1 { }
1517

0 commit comments

Comments
 (0)