From b3b65ee8e594408db143cd9e3f7b9bd29c744a79 Mon Sep 17 00:00:00 2001 From: mackysoft Date: Sat, 19 Aug 2023 19:31:12 +0900 Subject: [PATCH] SubclassSelectorDrawer support now custom property drawer --- .../Editor/SubclassSelectorDrawer.cs | 40 +++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/SubclassSelectorDrawer.cs b/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/SubclassSelectorDrawer.cs index a0f198e..6f38577 100644 --- a/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/SubclassSelectorDrawer.cs +++ b/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/SubclassSelectorDrawer.cs @@ -6,7 +6,8 @@ using UnityEngine; using UnityEditor; using UnityEditor.IMGUI.Controls; -namespace MackySoft.SerializeReferenceExtensions.Editor { +namespace MackySoft.SerializeReferenceExtensions.Editor +{ [CustomPropertyDrawer(typeof(SubclassSelectorAttribute))] public class SubclassSelectorDrawer : PropertyDrawer { @@ -46,8 +47,31 @@ namespace MackySoft.SerializeReferenceExtensions.Editor { popup.TypePopup.Show(popupPosition); } - // Draw the managed reference property. - EditorGUI.PropertyField(position,property,label,true); + // Check if a custom property drawer exists for this type. + PropertyDrawer customDrawer = GetCustomPropertyDrawer(property); + if (customDrawer != null) + { + // Draw the property with custom property drawer. + Rect foldoutRect = new Rect(position); + foldoutRect.height = EditorGUIUtility.singleLineHeight; + property.isExpanded = EditorGUI.Foldout(foldoutRect, property.isExpanded, label, true); + + if (property.isExpanded) + { + using (new EditorGUI.IndentLevelScope(1)) + { + Rect indentedRect = EditorGUI.IndentedRect(position); + float foldoutDifference = EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; + indentedRect.height -= foldoutDifference; + indentedRect.y += foldoutDifference; + customDrawer.OnGUI(indentedRect, property, label); + } + } + } + else + { + EditorGUI.PropertyField(position, property, label, true); + } } else { EditorGUI.LabelField(position,label,k_IsNotManagedReferenceLabel); } @@ -55,6 +79,16 @@ namespace MackySoft.SerializeReferenceExtensions.Editor { EditorGUI.EndProperty(); } + PropertyDrawer GetCustomPropertyDrawer (SerializedProperty property) + { + Type propertyType = ManagedReferenceUtility.GetType(property.managedReferenceFullTypename); + if (propertyType != null && PropertyDrawerCache.TryGetPropertyDrawer(propertyType, out PropertyDrawer drawer)) + { + return drawer; + } + return null; + } + TypePopupCache GetTypePopup (SerializedProperty property) { // Cache this string. This property internally call Assembly.GetName, which result in a large allocation. string managedReferenceFieldTypename = property.managedReferenceFieldTypename;