Skip to content

Commit 6dc9fbc

Browse files
committed
Improved performance of SubclassSelectorDrawer
# Changed - `SubclassSelectorDrawer` now caches the display name of type. - `SubclassSelectorDrawer` now finds the type via `UnityEditor.TypeCache`.
1 parent e020427 commit 6dc9fbc

6 files changed

+2468
-2410
lines changed

Assembly-CSharp-Editor.csproj

Lines changed: 636 additions & 627 deletions
Large diffs are not rendered by default.

Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/SubclassSelectorDrawer.cs

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ public TypePopupCache (AdvancedTypePopup typePopup,AdvancedDropdownState state)
2222

2323
const int k_MaxTypePopupLineCount = 13;
2424
static readonly Type k_UnityObjectType = typeof(UnityEngine.Object);
25+
static readonly GUIContent k_NullDisplayName = new GUIContent(TypeMenuUtility.k_NullDisplayName);
2526
static readonly GUIContent k_IsNotManagedReferenceLabel = new GUIContent("The property type is not manage reference.");
2627

2728
readonly Dictionary<string,TypePopupCache> m_TypePopups = new Dictionary<string,TypePopupCache>();
29+
readonly Dictionary<string,GUIContent> m_TypeNameCaches = new Dictionary<string,GUIContent>();
2830

2931
SerializedProperty m_TargetProperty;
3032

@@ -40,7 +42,7 @@ public override void OnGUI (Rect position,SerializedProperty property,GUIContent
4042
popupPosition.x += EditorGUIUtility.labelWidth;
4143
popupPosition.height = EditorGUIUtility.singleLineHeight;
4244

43-
if (EditorGUI.DropdownButton(popupPosition,new GUIContent(GetTypeName(property)),FocusType.Keyboard)) {
45+
if (EditorGUI.DropdownButton(popupPosition,GetTypeName(property),FocusType.Keyboard)) {
4446
m_TargetProperty = property;
4547
popup.TypePopup.Show(popupPosition);
4648
}
@@ -60,17 +62,14 @@ TypePopupCache GetTypePopup (SerializedProperty property) {
6062

6163
Type baseType = property.GetManagedReferenceFieldType();
6264
var popup = new AdvancedTypePopup(
63-
AppDomain.CurrentDomain.GetAssemblies()
64-
.SelectMany(assembly => assembly.GetTypes())
65-
.Where(p =>
66-
p.IsClass &&
67-
(p.IsPublic || p.IsNestedPublic) &&
68-
!p.IsAbstract &&
69-
!p.IsGenericType &&
70-
baseType.IsAssignableFrom(p) &&
71-
!k_UnityObjectType.IsAssignableFrom(p) &&
72-
Attribute.IsDefined(p,typeof(SerializableAttribute))
73-
),
65+
TypeCache.GetTypesDerivedFrom(baseType).Where(p =>
66+
p.IsClass &&
67+
(p.IsPublic || p.IsNestedPublic) &&
68+
!p.IsAbstract &&
69+
!p.IsGenericType &&
70+
!k_UnityObjectType.IsAssignableFrom(p) &&
71+
Attribute.IsDefined(p,typeof(SerializableAttribute))
72+
),
7473
k_MaxTypePopupLineCount,
7574
state
7675
);
@@ -86,22 +85,32 @@ TypePopupCache GetTypePopup (SerializedProperty property) {
8685
return result;
8786
}
8887

89-
static string GetTypeName (SerializedProperty property) {
88+
GUIContent GetTypeName (SerializedProperty property) {
9089
if (string.IsNullOrEmpty(property.managedReferenceFullTypename)) {
91-
return TypeMenuUtility.k_NullDisplayName;
90+
return k_NullDisplayName;
91+
}
92+
if (m_TypeNameCaches.TryGetValue(property.managedReferenceFullTypename,out GUIContent cachedTypeName)) {
93+
return cachedTypeName;
9294
}
9395

9496
Type type = property.GetManagedReferenceType();
97+
string typeName = null;
9598

9699
AddTypeMenuAttribute typeMenu = TypeMenuUtility.GetAttribute(type);
97100
if (typeMenu != null) {
98-
string typeName = typeMenu.GetTypeNameWithoutPath();
101+
typeName = typeMenu.GetTypeNameWithoutPath();
99102
if (!string.IsNullOrWhiteSpace(typeName)) {
100-
return ObjectNames.NicifyVariableName(typeName);
103+
typeName = ObjectNames.NicifyVariableName(typeName);
101104
}
102105
}
103106

104-
return ObjectNames.NicifyVariableName(type.Name);
107+
if (string.IsNullOrWhiteSpace(typeName)) {
108+
typeName = ObjectNames.NicifyVariableName(type.Name);
109+
}
110+
111+
GUIContent result = new GUIContent(typeName);
112+
m_TypeNameCaches.Add(property.managedReferenceFullTypename,result);
113+
return result;
105114
}
106115

107116
public override float GetPropertyHeight (SerializedProperty property,GUIContent label) {

0 commit comments

Comments
 (0)