Skip to content

Commit c1648be

Browse files
authored
fix: indentation issues in Unity 2022 (#63)
1 parent 4e41104 commit c1648be

File tree

6 files changed

+79
-8
lines changed

6 files changed

+79
-8
lines changed

Editor/Drawers/CustomObjectDrawer.cs

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
using UnityEditor;
1+
#if UNITY_2022_3_20 || UNITY_2022_3_21
2+
#define UNITY_2022_3_20_OR_NEWER
3+
using TNRD.Utilities;
4+
#endif
5+
using TNRD.Utilities;
6+
using UnityEditor;
27
using UnityEngine;
38

49
namespace TNRD.Drawers
@@ -22,24 +27,56 @@ public partial class CustomObjectDrawer
2227
public event DeletePressedDelegate DeletePressed;
2328
public event PropertiesClickedDelegate PropertiesClicked;
2429

25-
public void OnGUI(Rect position, GUIContent label, GUIContent content, SerializedProperty property)
30+
public void OnGUI(Rect position, GUIContent label, GUIContent content, SerializedProperty property, bool hasChildren)
2631
{
2732
Rect positionWithoutThumb = new Rect(position);
2833
positionWithoutThumb.xMax -= 20;
2934

30-
position = DrawPrefixLabel(position, label);
35+
position = DrawPrefixLabel(position, label, hasChildren);
3136
DrawObjectField(position, content);
3237
DrawButton(position, property);
3338

3439
HandleMouseDown(position, positionWithoutThumb, property);
3540
HandleKeyDown(property);
3641
}
3742

38-
private Rect DrawPrefixLabel(Rect position, GUIContent label)
43+
private Rect DrawPrefixLabel(Rect position, GUIContent label, bool hasChildren)
3944
{
45+
#if UNITY_2022_3_20_OR_NEWER
46+
GUIStyle labelStyle = isSelected ? Styles.SelectedLabelStyle : Styles.RegularLabelStyle;
47+
48+
if (hasChildren || EditorGUI.indentLevel >= 1)
49+
{
50+
using (new EditorGUI.IndentLevelScope(-1))
51+
{
52+
position.xMin -= ReflectedEditorGUI.indent;
53+
}
54+
}
55+
56+
Rect result = EditorGUI.PrefixLabel(position, label, labelStyle);
57+
58+
if (hasChildren || EditorGUI.indentLevel >= 1)
59+
{
60+
result.xMin -= ReflectedEditorGUI.indentWidth;
61+
}
62+
63+
return result;
64+
#elif UNITY_2022_2_OR_NEWER
65+
if (hasChildren || EditorGUI.indentLevel >= 1)
66+
{
67+
position.xMin += ReflectedEditorGUI.indentWidth;
68+
}
69+
70+
GUIStyle labelStyle = isSelected ? Styles.SelectedLabelStyle : Styles.RegularLabelStyle;
71+
Rect result = EditorGUI.PrefixLabel(position, label, labelStyle);
72+
if (hasChildren || EditorGUI.indentLevel>=1)
73+
result.xMin -= ReflectedEditorGUI.indentWidth;
74+
return result;
75+
#else
4076
GUIStyle labelStyle = isSelected ? Styles.SelectedLabelStyle : Styles.RegularLabelStyle;
4177
Rect result = EditorGUI.PrefixLabel(position, label, labelStyle);
4278
return result;
79+
#endif
4380
}
4481

4582
private void DrawObjectField(Rect position, GUIContent objectFieldContent)

Editor/Drawers/RawReferenceDrawer.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
using System;
1+
#if UNITY_2022_3_20 || UNITY_2022_3_21
2+
#define UNITY_2022_3_20_OR_NEWER
3+
#endif
4+
5+
using System;
26
using System.Reflection;
37
using TNRD.Utilities;
48
using UnityEditor;
@@ -45,7 +49,7 @@ public void OnGUI(Rect position)
4549
? EditorGUIUtility.ObjectContent((MonoScript)null, typeof(MonoScript))
4650
: new GUIContent(rawReferenceValue.GetType().Name, IconUtility.ScriptIcon);
4751

48-
CustomObjectDrawer.OnGUI(objectFieldRect, label, content, Property);
52+
CustomObjectDrawer.OnGUI(objectFieldRect, label, content, Property, rawReferenceValue != null);
4953

5054
HandleDragAndDrop(objectFieldRect);
5155

@@ -58,6 +62,9 @@ public void OnGUI(Rect position)
5862

5963
private void DrawProperty(Rect position)
6064
{
65+
#if UNITY_2022_3_20_OR_NEWER
66+
position.xMin -= ReflectedEditorGUI.indent;
67+
#endif
6168
EditorGUI.PropertyField(position,
6269
RawReferenceProperty,
6370
GUIContent.none,

Editor/Drawers/UnityReferenceDrawer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public void OnGUI(Rect position)
2727
Object unityReference = UnityReferenceProperty.objectReferenceValue;
2828
Type referenceType = unityReference == null ? typeof(Object) : unityReference.GetType();
2929
GUIContent objectContent = EditorGUIUtility.ObjectContent(unityReference, referenceType);
30-
CustomObjectDrawer.OnGUI(position, label, objectContent, Property);
30+
CustomObjectDrawer.OnGUI(position, label, objectContent, Property, false);
3131
HandleDragAndDrop(position);
3232
}
3333

Editor/SerializableInterfacePropertyDrawer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections;
33
using TNRD.Drawers;
4+
using TNRD.Utilities;
45
using UnityEditor;
56
using UnityEngine;
67
using UnityEngine.Assertions;
@@ -81,7 +82,7 @@ private Type GetGenericArgument()
8182

8283
private IReferenceDrawer GetReferenceDrawer(SerializedProperty property, GUIContent label)
8384
{
84-
SerializedProperty modeProperty = serializedProperty.FindPropertyRelative("mode");
85+
SerializedProperty modeProperty = serializedProperty.ReferenceModeProperty();
8586
ReferenceMode referenceMode = (ReferenceMode)modeProperty.enumValueIndex;
8687

8788
switch (referenceMode)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.Reflection;
2+
using UnityEditor;
3+
4+
namespace TNRD.Utilities
5+
{
6+
public static class ReflectedEditorGUI
7+
{
8+
private static readonly PropertyInfo indentLevelProperty =
9+
typeof(EditorGUI).GetProperty("indent", BindingFlags.Static | BindingFlags.NonPublic);
10+
11+
private static readonly FieldInfo indentWidthField =
12+
typeof(EditorGUI).GetField("kIndentPerLevel", BindingFlags.Static | BindingFlags.NonPublic);
13+
14+
static ReflectedEditorGUI()
15+
{
16+
indentWidth = (float)indentWidthField.GetValue(null);
17+
}
18+
19+
public static float indent => (float)indentLevelProperty.GetValue(null);
20+
21+
public static float indentWidth { get; private set; }
22+
}
23+
}

Editor/Utilities/ReflectedEditorGUI.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)