Skip to content

Commit f8b317b

Browse files
committed
Optimize drawing SubclassSelector
1 parent a10a5b9 commit f8b317b

File tree

2 files changed

+19
-30
lines changed

2 files changed

+19
-30
lines changed

Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/ManagedReferenceUtility.cs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,18 @@
66
namespace MackySoft.SerializeReferenceExtensions.Editor {
77
public static class ManagedReferenceUtility {
88

9-
public static Type GetManagedReferenceFieldType (this SerializedProperty property) {
10-
if (property.propertyType != SerializedPropertyType.ManagedReference) {
11-
throw SerializedPropertyTypeMustBeManagedReference(nameof(property));
12-
}
13-
return GetType(property.managedReferenceFieldTypename);
14-
}
15-
16-
public static Type GetManagedReferenceType (this SerializedProperty property) {
17-
if (property.propertyType != SerializedPropertyType.ManagedReference) {
18-
throw SerializedPropertyTypeMustBeManagedReference(nameof(property));
19-
}
20-
return GetType(property.managedReferenceFullTypename);
21-
}
22-
239
public static object SetManagedReference (this SerializedProperty property,Type type) {
2410
object obj = (type != null) ? Activator.CreateInstance(type) : null;
2511
property.managedReferenceValue = obj;
2612
return obj;
2713
}
2814

29-
static Type GetType (string typeName) {
15+
public static Type GetType (string typeName) {
3016
int splitIndex = typeName.IndexOf(' ');
3117
var assembly = Assembly.Load(typeName.Substring(0,splitIndex));
3218
return assembly.GetType(typeName.Substring(splitIndex + 1));
3319
}
3420

35-
static ArgumentException SerializedPropertyTypeMustBeManagedReference (string paramName) {
36-
return new ArgumentException("The serialized property type must be SerializedPropertyType.ManagedReference.",paramName);
37-
}
38-
3921
}
4022
}
4123
#endif

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

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using UnityEngine;
66
using UnityEditor;
77
using UnityEditor.IMGUI.Controls;
8+
using UnityEngine.UIElements;
9+
using UnityEditor.UIElements;
810

911
namespace MackySoft.SerializeReferenceExtensions.Editor {
1012

@@ -29,20 +31,19 @@ public TypePopupCache (AdvancedTypePopup typePopup,AdvancedDropdownState state)
2931
readonly Dictionary<string,GUIContent> m_TypeNameCaches = new Dictionary<string,GUIContent>();
3032

3133
SerializedProperty m_TargetProperty;
32-
34+
3335
public override void OnGUI (Rect position,SerializedProperty property,GUIContent label) {
3436
EditorGUI.BeginProperty(position,label,property);
3537

3638
if (property.propertyType == SerializedPropertyType.ManagedReference) {
37-
TypePopupCache popup = GetTypePopup(property);
38-
3939
// Draw the subclass selector popup.
4040
Rect popupPosition = new Rect(position);
4141
popupPosition.width -= EditorGUIUtility.labelWidth;
4242
popupPosition.x += EditorGUIUtility.labelWidth;
4343
popupPosition.height = EditorGUIUtility.singleLineHeight;
4444

4545
if (EditorGUI.DropdownButton(popupPosition,GetTypeName(property),FocusType.Keyboard)) {
46+
TypePopupCache popup = GetTypePopup(property);
4647
m_TargetProperty = property;
4748
popup.TypePopup.Show(popupPosition);
4849
}
@@ -57,10 +58,13 @@ public override void OnGUI (Rect position,SerializedProperty property,GUIContent
5758
}
5859

5960
TypePopupCache GetTypePopup (SerializedProperty property) {
60-
if (!m_TypePopups.TryGetValue(property.managedReferenceFieldTypename,out TypePopupCache result)) {
61-
var state = new AdvancedDropdownState();
61+
// Cache this string. This property internally call Assembly.GetName, which result in a large allocation.
62+
string managedReferenceFieldTypename = property.managedReferenceFieldTypename;
6263

63-
Type baseType = property.GetManagedReferenceFieldType();
64+
if (!m_TypePopups.TryGetValue(managedReferenceFieldTypename,out TypePopupCache result)) {
65+
var state = new AdvancedDropdownState();
66+
67+
Type baseType = ManagedReferenceUtility.GetType(managedReferenceFieldTypename);
6468
var popup = new AdvancedTypePopup(
6569
TypeCache.GetTypesDerivedFrom(baseType).Where(p =>
6670
(p.IsPublic || p.IsNestedPublic) &&
@@ -79,20 +83,23 @@ TypePopupCache GetTypePopup (SerializedProperty property) {
7983
m_TargetProperty.serializedObject.ApplyModifiedProperties();
8084
};
8185

82-
m_TypePopups.Add(property.managedReferenceFieldTypename,new TypePopupCache(popup,state));
86+
m_TypePopups.Add(managedReferenceFieldTypename,new TypePopupCache(popup,state));
8387
}
8488
return result;
8589
}
8690

8791
GUIContent GetTypeName (SerializedProperty property) {
88-
if (string.IsNullOrEmpty(property.managedReferenceFullTypename)) {
92+
// Cache this string.
93+
string managedReferenceFullTypename = property.managedReferenceFullTypename;
94+
95+
if (string.IsNullOrEmpty(managedReferenceFullTypename)) {
8996
return k_NullDisplayName;
9097
}
91-
if (m_TypeNameCaches.TryGetValue(property.managedReferenceFullTypename,out GUIContent cachedTypeName)) {
98+
if (m_TypeNameCaches.TryGetValue(managedReferenceFullTypename,out GUIContent cachedTypeName)) {
9299
return cachedTypeName;
93100
}
94101

95-
Type type = property.GetManagedReferenceType();
102+
Type type = ManagedReferenceUtility.GetType(managedReferenceFullTypename);
96103
string typeName = null;
97104

98105
AddTypeMenuAttribute typeMenu = TypeMenuUtility.GetAttribute(type);
@@ -108,7 +115,7 @@ GUIContent GetTypeName (SerializedProperty property) {
108115
}
109116

110117
GUIContent result = new GUIContent(typeName);
111-
m_TypeNameCaches.Add(property.managedReferenceFullTypename,result);
118+
m_TypeNameCaches.Add(managedReferenceFullTypename,result);
112119
return result;
113120
}
114121

0 commit comments

Comments
 (0)