From f8b317ba209df2d8601b834cd7ab858ed61bde22 Mon Sep 17 00:00:00 2001 From: mackysoft Date: Sat, 5 Mar 2022 02:00:34 +0900 Subject: [PATCH 1/4] Optimize drawing SubclassSelector --- .../Editor/ManagedReferenceUtility.cs | 20 +------------ .../Editor/SubclassSelectorDrawer.cs | 29 ++++++++++++------- 2 files changed, 19 insertions(+), 30 deletions(-) diff --git a/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/ManagedReferenceUtility.cs b/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/ManagedReferenceUtility.cs index d0acc71..dbf3205 100644 --- a/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/ManagedReferenceUtility.cs +++ b/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/ManagedReferenceUtility.cs @@ -6,36 +6,18 @@ using UnityEditor; namespace MackySoft.SerializeReferenceExtensions.Editor { public static class ManagedReferenceUtility { - public static Type GetManagedReferenceFieldType (this SerializedProperty property) { - if (property.propertyType != SerializedPropertyType.ManagedReference) { - throw SerializedPropertyTypeMustBeManagedReference(nameof(property)); - } - return GetType(property.managedReferenceFieldTypename); - } - - public static Type GetManagedReferenceType (this SerializedProperty property) { - if (property.propertyType != SerializedPropertyType.ManagedReference) { - throw SerializedPropertyTypeMustBeManagedReference(nameof(property)); - } - return GetType(property.managedReferenceFullTypename); - } - public static object SetManagedReference (this SerializedProperty property,Type type) { object obj = (type != null) ? Activator.CreateInstance(type) : null; property.managedReferenceValue = obj; return obj; } - static Type GetType (string typeName) { + public static Type GetType (string typeName) { int splitIndex = typeName.IndexOf(' '); var assembly = Assembly.Load(typeName.Substring(0,splitIndex)); return assembly.GetType(typeName.Substring(splitIndex + 1)); } - static ArgumentException SerializedPropertyTypeMustBeManagedReference (string paramName) { - return new ArgumentException("The serialized property type must be SerializedPropertyType.ManagedReference.",paramName); - } - } } #endif \ No newline at end of file diff --git a/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/SubclassSelectorDrawer.cs b/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/SubclassSelectorDrawer.cs index 0e5f746..46c5cd3 100644 --- a/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/SubclassSelectorDrawer.cs +++ b/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/SubclassSelectorDrawer.cs @@ -5,6 +5,8 @@ using System.Collections.Generic; using UnityEngine; using UnityEditor; using UnityEditor.IMGUI.Controls; +using UnityEngine.UIElements; +using UnityEditor.UIElements; namespace MackySoft.SerializeReferenceExtensions.Editor { @@ -29,13 +31,11 @@ namespace MackySoft.SerializeReferenceExtensions.Editor { readonly Dictionary m_TypeNameCaches = new Dictionary(); SerializedProperty m_TargetProperty; - + public override void OnGUI (Rect position,SerializedProperty property,GUIContent label) { EditorGUI.BeginProperty(position,label,property); if (property.propertyType == SerializedPropertyType.ManagedReference) { - TypePopupCache popup = GetTypePopup(property); - // Draw the subclass selector popup. Rect popupPosition = new Rect(position); popupPosition.width -= EditorGUIUtility.labelWidth; @@ -43,6 +43,7 @@ namespace MackySoft.SerializeReferenceExtensions.Editor { popupPosition.height = EditorGUIUtility.singleLineHeight; if (EditorGUI.DropdownButton(popupPosition,GetTypeName(property),FocusType.Keyboard)) { + TypePopupCache popup = GetTypePopup(property); m_TargetProperty = property; popup.TypePopup.Show(popupPosition); } @@ -57,10 +58,13 @@ namespace MackySoft.SerializeReferenceExtensions.Editor { } TypePopupCache GetTypePopup (SerializedProperty property) { - if (!m_TypePopups.TryGetValue(property.managedReferenceFieldTypename,out TypePopupCache result)) { - var state = new AdvancedDropdownState(); + // Cache this string. This property internally call Assembly.GetName, which result in a large allocation. + string managedReferenceFieldTypename = property.managedReferenceFieldTypename; - Type baseType = property.GetManagedReferenceFieldType(); + if (!m_TypePopups.TryGetValue(managedReferenceFieldTypename,out TypePopupCache result)) { + var state = new AdvancedDropdownState(); + + Type baseType = ManagedReferenceUtility.GetType(managedReferenceFieldTypename); var popup = new AdvancedTypePopup( TypeCache.GetTypesDerivedFrom(baseType).Where(p => (p.IsPublic || p.IsNestedPublic) && @@ -79,20 +83,23 @@ namespace MackySoft.SerializeReferenceExtensions.Editor { m_TargetProperty.serializedObject.ApplyModifiedProperties(); }; - m_TypePopups.Add(property.managedReferenceFieldTypename,new TypePopupCache(popup,state)); + m_TypePopups.Add(managedReferenceFieldTypename,new TypePopupCache(popup,state)); } return result; } GUIContent GetTypeName (SerializedProperty property) { - if (string.IsNullOrEmpty(property.managedReferenceFullTypename)) { + // Cache this string. + string managedReferenceFullTypename = property.managedReferenceFullTypename; + + if (string.IsNullOrEmpty(managedReferenceFullTypename)) { return k_NullDisplayName; } - if (m_TypeNameCaches.TryGetValue(property.managedReferenceFullTypename,out GUIContent cachedTypeName)) { + if (m_TypeNameCaches.TryGetValue(managedReferenceFullTypename,out GUIContent cachedTypeName)) { return cachedTypeName; } - Type type = property.GetManagedReferenceType(); + Type type = ManagedReferenceUtility.GetType(managedReferenceFullTypename); string typeName = null; AddTypeMenuAttribute typeMenu = TypeMenuUtility.GetAttribute(type); @@ -108,7 +115,7 @@ namespace MackySoft.SerializeReferenceExtensions.Editor { } GUIContent result = new GUIContent(typeName); - m_TypeNameCaches.Add(property.managedReferenceFullTypename,result); + m_TypeNameCaches.Add(managedReferenceFullTypename,result); return result; } From 112b5f945709adfe8e836478b768fb2ffe300f12 Mon Sep 17 00:00:00 2001 From: mackysoft Date: Sat, 5 Mar 2022 02:04:22 +0900 Subject: [PATCH 2/4] Add ExamplEditor Needed to reproduce issue --- Assembly-CSharp-Editor.csproj | 6 ++---- .../Example/ExampleAssets/Scripts/Editor.meta | 8 ++++++++ .../ExampleAssets/Scripts/Editor/ExampleEditor.cs | 13 +++++++++++++ .../Scripts/Editor/ExampleEditor.cs.meta | 11 +++++++++++ 4 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Example/ExampleAssets/Scripts/Editor.meta create mode 100644 Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Example/ExampleAssets/Scripts/Editor/ExampleEditor.cs create mode 100644 Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Example/ExampleAssets/Scripts/Editor/ExampleEditor.cs.meta diff --git a/Assembly-CSharp-Editor.csproj b/Assembly-CSharp-Editor.csproj index 2872553..4b4196e 100644 --- a/Assembly-CSharp-Editor.csproj +++ b/Assembly-CSharp-Editor.csproj @@ -1,7 +1,7 @@  - 8.0 + latest Debug @@ -52,11 +52,9 @@ StandaloneWindows:5 2020.3.24f1 - - - + diff --git a/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Example/ExampleAssets/Scripts/Editor.meta b/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Example/ExampleAssets/Scripts/Editor.meta new file mode 100644 index 0000000..ef7a0c4 --- /dev/null +++ b/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Example/ExampleAssets/Scripts/Editor.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 9a6c0659b60e6c44f998f2d392bb5af6 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Example/ExampleAssets/Scripts/Editor/ExampleEditor.cs b/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Example/ExampleAssets/Scripts/Editor/ExampleEditor.cs new file mode 100644 index 0000000..2925c45 --- /dev/null +++ b/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Example/ExampleAssets/Scripts/Editor/ExampleEditor.cs @@ -0,0 +1,13 @@ +using UnityEditor; + +namespace MackySoft.SerializeReferenceExtensions.Example.Editor { + + [CustomEditor(typeof(Example))] + public class ExampleEditor : UnityEditor.Editor { + + // Enabling the custom editor slowdown rendering performance. + public override void OnInspectorGUI () { + base.OnInspectorGUI(); + } + } +} \ No newline at end of file diff --git a/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Example/ExampleAssets/Scripts/Editor/ExampleEditor.cs.meta b/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Example/ExampleAssets/Scripts/Editor/ExampleEditor.cs.meta new file mode 100644 index 0000000..a514b70 --- /dev/null +++ b/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Example/ExampleAssets/Scripts/Editor/ExampleEditor.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: cfc194a2f8ebd084b9a5e0be5ec30589 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: From e7a8195f17c598290cc1b256cf01025ae2b4caa7 Mon Sep 17 00:00:00 2001 From: mackysoft Date: Sat, 5 Mar 2022 02:15:38 +0900 Subject: [PATCH 3/4] Update ExampleEditor.cs --- .../Example/ExampleAssets/Scripts/Editor/ExampleEditor.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Example/ExampleAssets/Scripts/Editor/ExampleEditor.cs b/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Example/ExampleAssets/Scripts/Editor/ExampleEditor.cs index 2925c45..8cea2c4 100644 --- a/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Example/ExampleAssets/Scripts/Editor/ExampleEditor.cs +++ b/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Example/ExampleAssets/Scripts/Editor/ExampleEditor.cs @@ -2,10 +2,10 @@ using UnityEditor; namespace MackySoft.SerializeReferenceExtensions.Example.Editor { - [CustomEditor(typeof(Example))] + // Enabling the custom editor slowdown rendering performance. + //[CustomEditor(typeof(Example))] public class ExampleEditor : UnityEditor.Editor { - // Enabling the custom editor slowdown rendering performance. public override void OnInspectorGUI () { base.OnInspectorGUI(); } From 4cdcf995f6a712346be1f0bcd63ff0d20ad071bb Mon Sep 17 00:00:00 2001 From: mackysoft Date: Mon, 28 Mar 2022 23:06:11 +0900 Subject: [PATCH 4/4] Remove unnecessary namespaces --- .../Editor/SubclassSelectorDrawer.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/SubclassSelectorDrawer.cs b/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/SubclassSelectorDrawer.cs index 46c5cd3..88be58e 100644 --- a/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/SubclassSelectorDrawer.cs +++ b/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/SubclassSelectorDrawer.cs @@ -5,8 +5,6 @@ using System.Collections.Generic; using UnityEngine; using UnityEditor; using UnityEditor.IMGUI.Controls; -using UnityEngine.UIElements; -using UnityEditor.UIElements; namespace MackySoft.SerializeReferenceExtensions.Editor {