55using UnityEngine ;
66using UnityEditor ;
77using UnityEditor . IMGUI . Controls ;
8+ using UnityEngine . UIElements ;
9+ using UnityEditor . UIElements ;
810
911namespace 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