Improved performance of SubclassSelectorDrawer

# Changed
- `SubclassSelectorDrawer` now caches the display name of type.
- `SubclassSelectorDrawer` now finds the type via `UnityEditor.TypeCache`.
This commit is contained in:
mackysoft 2021-05-18 01:22:16 +09:00
parent e02042777f
commit 6dc9fbcaae
6 changed files with 2468 additions and 2410 deletions

File diff suppressed because it is too large Load Diff

View File

@ -22,9 +22,11 @@ namespace MackySoft.SerializeReferenceExtensions.Editor {
const int k_MaxTypePopupLineCount = 13; const int k_MaxTypePopupLineCount = 13;
static readonly Type k_UnityObjectType = typeof(UnityEngine.Object); static readonly Type k_UnityObjectType = typeof(UnityEngine.Object);
static readonly GUIContent k_NullDisplayName = new GUIContent(TypeMenuUtility.k_NullDisplayName);
static readonly GUIContent k_IsNotManagedReferenceLabel = new GUIContent("The property type is not manage reference."); static readonly GUIContent k_IsNotManagedReferenceLabel = new GUIContent("The property type is not manage reference.");
readonly Dictionary<string,TypePopupCache> m_TypePopups = new Dictionary<string,TypePopupCache>(); readonly Dictionary<string,TypePopupCache> m_TypePopups = new Dictionary<string,TypePopupCache>();
readonly Dictionary<string,GUIContent> m_TypeNameCaches = new Dictionary<string,GUIContent>();
SerializedProperty m_TargetProperty; SerializedProperty m_TargetProperty;
@ -40,7 +42,7 @@ namespace MackySoft.SerializeReferenceExtensions.Editor {
popupPosition.x += EditorGUIUtility.labelWidth; popupPosition.x += EditorGUIUtility.labelWidth;
popupPosition.height = EditorGUIUtility.singleLineHeight; popupPosition.height = EditorGUIUtility.singleLineHeight;
if (EditorGUI.DropdownButton(popupPosition,new GUIContent(GetTypeName(property)),FocusType.Keyboard)) { if (EditorGUI.DropdownButton(popupPosition,GetTypeName(property),FocusType.Keyboard)) {
m_TargetProperty = property; m_TargetProperty = property;
popup.TypePopup.Show(popupPosition); popup.TypePopup.Show(popupPosition);
} }
@ -60,17 +62,14 @@ namespace MackySoft.SerializeReferenceExtensions.Editor {
Type baseType = property.GetManagedReferenceFieldType(); Type baseType = property.GetManagedReferenceFieldType();
var popup = new AdvancedTypePopup( var popup = new AdvancedTypePopup(
AppDomain.CurrentDomain.GetAssemblies() TypeCache.GetTypesDerivedFrom(baseType).Where(p =>
.SelectMany(assembly => assembly.GetTypes()) p.IsClass &&
.Where(p => (p.IsPublic || p.IsNestedPublic) &&
p.IsClass && !p.IsAbstract &&
(p.IsPublic || p.IsNestedPublic) && !p.IsGenericType &&
!p.IsAbstract && !k_UnityObjectType.IsAssignableFrom(p) &&
!p.IsGenericType && Attribute.IsDefined(p,typeof(SerializableAttribute))
baseType.IsAssignableFrom(p) && ),
!k_UnityObjectType.IsAssignableFrom(p) &&
Attribute.IsDefined(p,typeof(SerializableAttribute))
),
k_MaxTypePopupLineCount, k_MaxTypePopupLineCount,
state state
); );
@ -86,22 +85,32 @@ namespace MackySoft.SerializeReferenceExtensions.Editor {
return result; return result;
} }
static string GetTypeName (SerializedProperty property) { GUIContent GetTypeName (SerializedProperty property) {
if (string.IsNullOrEmpty(property.managedReferenceFullTypename)) { if (string.IsNullOrEmpty(property.managedReferenceFullTypename)) {
return TypeMenuUtility.k_NullDisplayName; return k_NullDisplayName;
}
if (m_TypeNameCaches.TryGetValue(property.managedReferenceFullTypename,out GUIContent cachedTypeName)) {
return cachedTypeName;
} }
Type type = property.GetManagedReferenceType(); Type type = property.GetManagedReferenceType();
string typeName = null;
AddTypeMenuAttribute typeMenu = TypeMenuUtility.GetAttribute(type); AddTypeMenuAttribute typeMenu = TypeMenuUtility.GetAttribute(type);
if (typeMenu != null) { if (typeMenu != null) {
string typeName = typeMenu.GetTypeNameWithoutPath(); typeName = typeMenu.GetTypeNameWithoutPath();
if (!string.IsNullOrWhiteSpace(typeName)) { if (!string.IsNullOrWhiteSpace(typeName)) {
return ObjectNames.NicifyVariableName(typeName); typeName = ObjectNames.NicifyVariableName(typeName);
} }
} }
return ObjectNames.NicifyVariableName(type.Name); if (string.IsNullOrWhiteSpace(typeName)) {
typeName = ObjectNames.NicifyVariableName(type.Name);
}
GUIContent result = new GUIContent(typeName);
m_TypeNameCaches.Add(property.managedReferenceFullTypename,result);
return result;
} }
public override float GetPropertyHeight (SerializedProperty property,GUIContent label) { public override float GetPropertyHeight (SerializedProperty property,GUIContent label) {

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
 
Microsoft Visual Studio Solution File, Format Version 11.00 Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2010 # Visual Studio 15
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MackySoft.SerializeReferenceExtensions", "MackySoft.SerializeReferenceExtensions.csproj", "{416A0403-CB55-36CC-5B0D-0D345FA05F49}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MackySoft.SerializeReferenceExtensions", "MackySoft.SerializeReferenceExtensions.csproj", "{416A0403-CB55-36CC-5B0D-0D345FA05F49}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MackySoft.SerializeReferenceExtensions.Example", "MackySoft.SerializeReferenceExtensions.Example.csproj", "{19A3DFD2-76AC-5BD2-796C-431D7979E720}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MackySoft.SerializeReferenceExtensions.Example", "MackySoft.SerializeReferenceExtensions.Example.csproj", "{19A3DFD2-76AC-5BD2-796C-431D7979E720}"