Skip to content

Commit e5627b1

Browse files
authored
Merge pull request #54 from mackysoft/fix/dropdown-overlap
Fix dropdown overlap in Unity 2023.2
2 parents 7f424c0 + 5f54c25 commit e5627b1

File tree

4 files changed

+88
-22
lines changed

4 files changed

+88
-22
lines changed

Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/ManagedReferenceUtility.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@
44
using UnityEditor;
55
using UnityEngine;
66

7-
namespace MackySoft.SerializeReferenceExtensions.Editor {
7+
namespace MackySoft.SerializeReferenceExtensions.Editor
8+
{
9+
810
public static class ManagedReferenceUtility {
911

1012
public static object SetManagedReference (this SerializedProperty property,Type type) {
1113
object result = null;
1214

1315
#if UNITY_2021_3_OR_NEWER
1416
// NOTE: managedReferenceValue getter is available only in Unity 2021.3 or later.
15-
if (property.managedReferenceValue != null && type != null)
17+
if ((type != null) && (property.managedReferenceValue != null))
1618
{
1719
// Restore an previous values from json.
1820
string json = JsonUtility.ToJson(property.managedReferenceValue);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#if UNITY_2019_3_OR_NEWER
2+
using System.Collections.Generic;
3+
using UnityEditor;
4+
5+
namespace MackySoft.SerializeReferenceExtensions.Editor
6+
{
7+
public static class SerializedPropertyExtensions
8+
{
9+
public static IEnumerable<SerializedProperty> GetChildProperties (this SerializedProperty parent, int depth = 1)
10+
{
11+
parent = parent.Copy();
12+
13+
int depthOfParent = parent.depth;
14+
var enumerator = parent.GetEnumerator();
15+
16+
while (enumerator.MoveNext())
17+
{
18+
if (enumerator.Current is not SerializedProperty childProperty)
19+
{
20+
continue;
21+
}
22+
if (childProperty.depth > (depthOfParent + depth))
23+
{
24+
continue;
25+
}
26+
yield return childProperty.Copy();
27+
}
28+
}
29+
}
30+
}
31+
#endif

Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/SerializedPropertyExtensions.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.

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

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,51 +31,74 @@ public TypePopupCache (AdvancedTypePopup typePopup,AdvancedDropdownState state)
3131

3232
SerializedProperty m_TargetProperty;
3333

34-
public override void OnGUI (Rect position,SerializedProperty property,GUIContent label) {
35-
EditorGUI.BeginProperty(position,label,property);
36-
37-
if (property.propertyType == SerializedPropertyType.ManagedReference) {
34+
public override void OnGUI (Rect position, SerializedProperty property, GUIContent label)
35+
{
36+
EditorGUI.BeginProperty(position, label, property);
3837

39-
// render label first to avoid label overlap for lists
38+
if (property.propertyType == SerializedPropertyType.ManagedReference)
39+
{
40+
// Render label first to avoid label overlap for lists
4041
Rect foldoutLabelRect = new Rect(position);
4142
foldoutLabelRect.height = EditorGUIUtility.singleLineHeight;
42-
foldoutLabelRect.x += EditorGUI.indentLevel * 12;
43+
foldoutLabelRect = EditorGUI.IndentedRect(foldoutLabelRect);
4344
Rect popupPosition = EditorGUI.PrefixLabel(foldoutLabelRect, label);
4445

4546
// Draw the subclass selector popup.
46-
if (EditorGUI.DropdownButton(popupPosition,GetTypeName(property),FocusType.Keyboard)) {
47+
if (EditorGUI.DropdownButton(popupPosition, GetTypeName(property), FocusType.Keyboard))
48+
{
4749
TypePopupCache popup = GetTypePopup(property);
4850
m_TargetProperty = property;
4951
popup.TypePopup.Show(popupPosition);
5052
}
5153

52-
// Check if a custom property drawer exists for this type.
53-
PropertyDrawer customDrawer = GetCustomPropertyDrawer(property);
54-
if (customDrawer != null)
54+
// Draw the foldout.
55+
if (!string.IsNullOrEmpty(property.managedReferenceFullTypename))
5556
{
56-
// Draw the property with custom property drawer.
5757
Rect foldoutRect = new Rect(position);
5858
foldoutRect.height = EditorGUIUtility.singleLineHeight;
59+
foldoutRect.x -= 12;
5960
property.isExpanded = EditorGUI.Foldout(foldoutRect, property.isExpanded, GUIContent.none, true);
61+
}
6062

61-
if (property.isExpanded)
63+
// Draw property if expanded.
64+
if (property.isExpanded)
65+
{
66+
using (new EditorGUI.IndentLevelScope())
6267
{
63-
using (new EditorGUI.IndentLevelScope())
68+
// Check if a custom property drawer exists for this type.
69+
PropertyDrawer customDrawer = GetCustomPropertyDrawer(property);
70+
if (customDrawer != null)
6471
{
72+
// Draw the property with custom property drawer.
6573
Rect indentedRect = position;
6674
float foldoutDifference = EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
6775
indentedRect.height = customDrawer.GetPropertyHeight(property, label);
6876
indentedRect.y += foldoutDifference;
6977
customDrawer.OnGUI(indentedRect, property, label);
7078
}
79+
else
80+
{
81+
// Draw the properties of the child elements.
82+
// NOTE: In the following code, since the foldout layout isn't working properly, I'll iterate through the properties of the child elements myself.
83+
// EditorGUI.PropertyField(position, property, GUIContent.none, true);
84+
85+
Rect childPosition = position;
86+
childPosition.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
87+
foreach (SerializedProperty childProperty in property.GetChildProperties())
88+
{
89+
float height = EditorGUI.GetPropertyHeight(childProperty, new GUIContent(childProperty.displayName, childProperty.tooltip), true);
90+
childPosition.height = height;
91+
EditorGUI.PropertyField(childPosition, childProperty, true);
92+
93+
childPosition.y += height + EditorGUIUtility.standardVerticalSpacing;
94+
}
95+
}
7196
}
7297
}
73-
else
74-
{
75-
EditorGUI.PropertyField(position, property, GUIContent.none, true);
76-
}
77-
} else {
78-
EditorGUI.LabelField(position,label,k_IsNotManagedReferenceLabel);
98+
}
99+
else
100+
{
101+
EditorGUI.LabelField(position, label, k_IsNotManagedReferenceLabel);
79102
}
80103

81104
EditorGUI.EndProperty();
@@ -117,7 +140,6 @@ TypePopupCache GetTypePopup (SerializedProperty property) {
117140
foreach (var targetObject in m_TargetProperty.serializedObject.targetObjects) {
118141
SerializedObject individualObject = new SerializedObject(targetObject);
119142
SerializedProperty individualProperty = individualObject.FindProperty(m_TargetProperty.propertyPath);
120-
121143
object obj = individualProperty.SetManagedReference(type);
122144
individualProperty.isExpanded = (obj != null);
123145

0 commit comments

Comments
 (0)