Merge pull request #7 from mackysoft/fix/subclassselector-drawing-overhead
Improve `SubclassSelectorDrawer` performance
This commit is contained in:
commit
55a071abe9
@ -1,7 +1,7 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<LangVersion>8.0</LangVersion>
|
||||
<LangVersion>latest</LangVersion>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
@ -52,11 +52,9 @@
|
||||
<UnityBuildTarget>StandaloneWindows:5</UnityBuildTarget>
|
||||
<UnityVersion>2020.3.24f1</UnityVersion>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<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" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Assets\PackageTools\Editor\UnityPackageExporter.cs" />
|
||||
<Compile Include="Assets\MackySoft\MackySoft.SerializeReferenceExtensions\Example\ExampleAssets\Scripts\Editor\ExampleEditor.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="UnityEngine">
|
||||
|
||||
@ -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
|
||||
@ -29,13 +29,11 @@ namespace MackySoft.SerializeReferenceExtensions.Editor {
|
||||
readonly Dictionary<string,GUIContent> m_TypeNameCaches = new Dictionary<string,GUIContent>();
|
||||
|
||||
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 +41,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 +56,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 +81,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 +113,7 @@ namespace MackySoft.SerializeReferenceExtensions.Editor {
|
||||
}
|
||||
|
||||
GUIContent result = new GUIContent(typeName);
|
||||
m_TypeNameCaches.Add(property.managedReferenceFullTypename,result);
|
||||
m_TypeNameCaches.Add(managedReferenceFullTypename,result);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9a6c0659b60e6c44f998f2d392bb5af6
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -0,0 +1,13 @@
|
||||
using UnityEditor;
|
||||
|
||||
namespace MackySoft.SerializeReferenceExtensions.Example.Editor {
|
||||
|
||||
// Enabling the custom editor slowdown rendering performance.
|
||||
//[CustomEditor(typeof(Example))]
|
||||
public class ExampleEditor : UnityEditor.Editor {
|
||||
|
||||
public override void OnInspectorGUI () {
|
||||
base.OnInspectorGUI();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cfc194a2f8ebd084b9a5e0be5ec30589
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Loading…
x
Reference in New Issue
Block a user