Skip to content

Commit b3b65ee

Browse files
committed
SubclassSelectorDrawer support now custom property drawer
1 parent 92c16ed commit b3b65ee

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/SubclassSelectorDrawer.cs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
using UnityEditor;
77
using UnityEditor.IMGUI.Controls;
88

9-
namespace MackySoft.SerializeReferenceExtensions.Editor {
9+
namespace MackySoft.SerializeReferenceExtensions.Editor
10+
{
1011

1112
[CustomPropertyDrawer(typeof(SubclassSelectorAttribute))]
1213
public class SubclassSelectorDrawer : PropertyDrawer {
@@ -46,15 +47,48 @@ public override void OnGUI (Rect position,SerializedProperty property,GUIContent
4647
popup.TypePopup.Show(popupPosition);
4748
}
4849

49-
// Draw the managed reference property.
50-
EditorGUI.PropertyField(position,property,label,true);
50+
// Check if a custom property drawer exists for this type.
51+
PropertyDrawer customDrawer = GetCustomPropertyDrawer(property);
52+
if (customDrawer != null)
53+
{
54+
// Draw the property with custom property drawer.
55+
Rect foldoutRect = new Rect(position);
56+
foldoutRect.height = EditorGUIUtility.singleLineHeight;
57+
property.isExpanded = EditorGUI.Foldout(foldoutRect, property.isExpanded, label, true);
58+
59+
if (property.isExpanded)
60+
{
61+
using (new EditorGUI.IndentLevelScope(1))
62+
{
63+
Rect indentedRect = EditorGUI.IndentedRect(position);
64+
float foldoutDifference = EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
65+
indentedRect.height -= foldoutDifference;
66+
indentedRect.y += foldoutDifference;
67+
customDrawer.OnGUI(indentedRect, property, label);
68+
}
69+
}
70+
}
71+
else
72+
{
73+
EditorGUI.PropertyField(position, property, label, true);
74+
}
5175
} else {
5276
EditorGUI.LabelField(position,label,k_IsNotManagedReferenceLabel);
5377
}
5478

5579
EditorGUI.EndProperty();
5680
}
5781

82+
PropertyDrawer GetCustomPropertyDrawer (SerializedProperty property)
83+
{
84+
Type propertyType = ManagedReferenceUtility.GetType(property.managedReferenceFullTypename);
85+
if (propertyType != null && PropertyDrawerCache.TryGetPropertyDrawer(propertyType, out PropertyDrawer drawer))
86+
{
87+
return drawer;
88+
}
89+
return null;
90+
}
91+
5892
TypePopupCache GetTypePopup (SerializedProperty property) {
5993
// Cache this string. This property internally call Assembly.GetName, which result in a large allocation.
6094
string managedReferenceFieldTypename = property.managedReferenceFieldTypename;

0 commit comments

Comments
 (0)