5
5
using UnityEngine ;
6
6
using UnityEditor ;
7
7
using UnityEditor . IMGUI . Controls ;
8
+ using UnityEngine . UIElements ;
9
+ using UnityEditor . UIElements ;
8
10
9
11
namespace MackySoft . SerializeReferenceExtensions . Editor {
10
12
@@ -29,20 +31,19 @@ public TypePopupCache (AdvancedTypePopup typePopup,AdvancedDropdownState state)
29
31
readonly Dictionary < string , GUIContent > m_TypeNameCaches = new Dictionary < string , GUIContent > ( ) ;
30
32
31
33
SerializedProperty m_TargetProperty ;
32
-
34
+
33
35
public override void OnGUI ( Rect position , SerializedProperty property , GUIContent label ) {
34
36
EditorGUI . BeginProperty ( position , label , property ) ;
35
37
36
38
if ( property . propertyType == SerializedPropertyType . ManagedReference ) {
37
- TypePopupCache popup = GetTypePopup ( property ) ;
38
-
39
39
// Draw the subclass selector popup.
40
40
Rect popupPosition = new Rect ( position ) ;
41
41
popupPosition . width -= EditorGUIUtility . labelWidth ;
42
42
popupPosition . x += EditorGUIUtility . labelWidth ;
43
43
popupPosition . height = EditorGUIUtility . singleLineHeight ;
44
44
45
45
if ( EditorGUI . DropdownButton ( popupPosition , GetTypeName ( property ) , FocusType . Keyboard ) ) {
46
+ TypePopupCache popup = GetTypePopup ( property ) ;
46
47
m_TargetProperty = property ;
47
48
popup . TypePopup . Show ( popupPosition ) ;
48
49
}
@@ -57,10 +58,13 @@ public override void OnGUI (Rect position,SerializedProperty property,GUIContent
57
58
}
58
59
59
60
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 ;
62
63
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 ) ;
64
68
var popup = new AdvancedTypePopup (
65
69
TypeCache . GetTypesDerivedFrom ( baseType ) . Where ( p =>
66
70
( p . IsPublic || p . IsNestedPublic ) &&
@@ -79,20 +83,23 @@ TypePopupCache GetTypePopup (SerializedProperty property) {
79
83
m_TargetProperty . serializedObject . ApplyModifiedProperties ( ) ;
80
84
} ;
81
85
82
- m_TypePopups . Add ( property . managedReferenceFieldTypename , new TypePopupCache ( popup , state ) ) ;
86
+ m_TypePopups . Add ( managedReferenceFieldTypename , new TypePopupCache ( popup , state ) ) ;
83
87
}
84
88
return result ;
85
89
}
86
90
87
91
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 ) ) {
89
96
return k_NullDisplayName ;
90
97
}
91
- if ( m_TypeNameCaches . TryGetValue ( property . managedReferenceFullTypename , out GUIContent cachedTypeName ) ) {
98
+ if ( m_TypeNameCaches . TryGetValue ( managedReferenceFullTypename , out GUIContent cachedTypeName ) ) {
92
99
return cachedTypeName ;
93
100
}
94
101
95
- Type type = property . GetManagedReferenceType ( ) ;
102
+ Type type = ManagedReferenceUtility . GetType ( managedReferenceFullTypename ) ;
96
103
string typeName = null ;
97
104
98
105
AddTypeMenuAttribute typeMenu = TypeMenuUtility . GetAttribute ( type ) ;
@@ -108,7 +115,7 @@ GUIContent GetTypeName (SerializedProperty property) {
108
115
}
109
116
110
117
GUIContent result = new GUIContent ( typeName ) ;
111
- m_TypeNameCaches . Add ( property . managedReferenceFullTypename , result ) ;
118
+ m_TypeNameCaches . Add ( managedReferenceFullTypename , result ) ;
112
119
return result ;
113
120
}
114
121
0 commit comments