Skip to content

Commit 55a071a

Browse files
authored
Merge pull request #7 from mackysoft/fix/subclassselector-drawing-overhead
Improve `SubclassSelectorDrawer` performance
2 parents cf8b8e3 + 4cdcf99 commit 55a071a

File tree

6 files changed

+51
-34
lines changed

6 files changed

+51
-34
lines changed

Assembly-CSharp-Editor.csproj

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<PropertyGroup>
4-
<LangVersion>8.0</LangVersion>
4+
<LangVersion>latest</LangVersion>
55
</PropertyGroup>
66
<PropertyGroup>
77
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -52,11 +52,9 @@
5252
<UnityBuildTarget>StandaloneWindows:5</UnityBuildTarget>
5353
<UnityVersion>2020.3.24f1</UnityVersion>
5454
</PropertyGroup>
55-
<ItemGroup>
56-
<Analyzer Include="C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\Extensions\Microsoft\Visual Studio Tools for Unity\Analyzers\Microsoft.Unity.Analyzers.dll" />
57-
</ItemGroup>
5855
<ItemGroup>
5956
<Compile Include="Assets\PackageTools\Editor\UnityPackageExporter.cs" />
57+
<Compile Include="Assets\MackySoft\MackySoft.SerializeReferenceExtensions\Example\ExampleAssets\Scripts\Editor\ExampleEditor.cs" />
6058
</ItemGroup>
6159
<ItemGroup>
6260
<Reference Include="UnityEngine">

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: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,19 @@ public TypePopupCache (AdvancedTypePopup typePopup,AdvancedDropdownState state)
2929
readonly Dictionary<string,GUIContent> m_TypeNameCaches = new Dictionary<string,GUIContent>();
3030

3131
SerializedProperty m_TargetProperty;
32-
32+
3333
public override void OnGUI (Rect position,SerializedProperty property,GUIContent label) {
3434
EditorGUI.BeginProperty(position,label,property);
3535

3636
if (property.propertyType == SerializedPropertyType.ManagedReference) {
37-
TypePopupCache popup = GetTypePopup(property);
38-
3937
// Draw the subclass selector popup.
4038
Rect popupPosition = new Rect(position);
4139
popupPosition.width -= EditorGUIUtility.labelWidth;
4240
popupPosition.x += EditorGUIUtility.labelWidth;
4341
popupPosition.height = EditorGUIUtility.singleLineHeight;
4442

4543
if (EditorGUI.DropdownButton(popupPosition,GetTypeName(property),FocusType.Keyboard)) {
44+
TypePopupCache popup = GetTypePopup(property);
4645
m_TargetProperty = property;
4746
popup.TypePopup.Show(popupPosition);
4847
}
@@ -57,10 +56,13 @@ public override void OnGUI (Rect position,SerializedProperty property,GUIContent
5756
}
5857

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

63-
Type baseType = property.GetManagedReferenceFieldType();
62+
if (!m_TypePopups.TryGetValue(managedReferenceFieldTypename,out TypePopupCache result)) {
63+
var state = new AdvancedDropdownState();
64+
65+
Type baseType = ManagedReferenceUtility.GetType(managedReferenceFieldTypename);
6466
var popup = new AdvancedTypePopup(
6567
TypeCache.GetTypesDerivedFrom(baseType).Where(p =>
6668
(p.IsPublic || p.IsNestedPublic) &&
@@ -79,20 +81,23 @@ TypePopupCache GetTypePopup (SerializedProperty property) {
7981
m_TargetProperty.serializedObject.ApplyModifiedProperties();
8082
};
8183

82-
m_TypePopups.Add(property.managedReferenceFieldTypename,new TypePopupCache(popup,state));
84+
m_TypePopups.Add(managedReferenceFieldTypename,new TypePopupCache(popup,state));
8385
}
8486
return result;
8587
}
8688

8789
GUIContent GetTypeName (SerializedProperty property) {
88-
if (string.IsNullOrEmpty(property.managedReferenceFullTypename)) {
90+
// Cache this string.
91+
string managedReferenceFullTypename = property.managedReferenceFullTypename;
92+
93+
if (string.IsNullOrEmpty(managedReferenceFullTypename)) {
8994
return k_NullDisplayName;
9095
}
91-
if (m_TypeNameCaches.TryGetValue(property.managedReferenceFullTypename,out GUIContent cachedTypeName)) {
96+
if (m_TypeNameCaches.TryGetValue(managedReferenceFullTypename,out GUIContent cachedTypeName)) {
9297
return cachedTypeName;
9398
}
9499

95-
Type type = property.GetManagedReferenceType();
100+
Type type = ManagedReferenceUtility.GetType(managedReferenceFullTypename);
96101
string typeName = null;
97102

98103
AddTypeMenuAttribute typeMenu = TypeMenuUtility.GetAttribute(type);
@@ -108,7 +113,7 @@ GUIContent GetTypeName (SerializedProperty property) {
108113
}
109114

110115
GUIContent result = new GUIContent(typeName);
111-
m_TypeNameCaches.Add(property.managedReferenceFullTypename,result);
116+
m_TypeNameCaches.Add(managedReferenceFullTypename,result);
112117
return result;
113118
}
114119

Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Example/ExampleAssets/Scripts/Editor.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using UnityEditor;
2+
3+
namespace MackySoft.SerializeReferenceExtensions.Example.Editor {
4+
5+
// Enabling the custom editor slowdown rendering performance.
6+
//[CustomEditor(typeof(Example))]
7+
public class ExampleEditor : UnityEditor.Editor {
8+
9+
public override void OnInspectorGUI () {
10+
base.OnInspectorGUI();
11+
}
12+
}
13+
}

Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Example/ExampleAssets/Scripts/Editor/ExampleEditor.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)