Skip to content

Commit

Permalink
fix: indentation issues in Unity 2022 (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
Thundernerd committed Mar 16, 2024
1 parent 4e41104 commit c1648be
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 8 deletions.
45 changes: 41 additions & 4 deletions Editor/Drawers/CustomObjectDrawer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
using UnityEditor;
#if UNITY_2022_3_20 || UNITY_2022_3_21
#define UNITY_2022_3_20_OR_NEWER
using TNRD.Utilities;
#endif
using TNRD.Utilities;
using UnityEditor;
using UnityEngine;

namespace TNRD.Drawers
Expand All @@ -22,24 +27,56 @@ public partial class CustomObjectDrawer
public event DeletePressedDelegate DeletePressed;
public event PropertiesClickedDelegate PropertiesClicked;

public void OnGUI(Rect position, GUIContent label, GUIContent content, SerializedProperty property)
public void OnGUI(Rect position, GUIContent label, GUIContent content, SerializedProperty property, bool hasChildren)
{
Rect positionWithoutThumb = new Rect(position);
positionWithoutThumb.xMax -= 20;

position = DrawPrefixLabel(position, label);
position = DrawPrefixLabel(position, label, hasChildren);
DrawObjectField(position, content);
DrawButton(position, property);

HandleMouseDown(position, positionWithoutThumb, property);
HandleKeyDown(property);
}

private Rect DrawPrefixLabel(Rect position, GUIContent label)
private Rect DrawPrefixLabel(Rect position, GUIContent label, bool hasChildren)
{
#if UNITY_2022_3_20_OR_NEWER
GUIStyle labelStyle = isSelected ? Styles.SelectedLabelStyle : Styles.RegularLabelStyle;

if (hasChildren || EditorGUI.indentLevel >= 1)
{
using (new EditorGUI.IndentLevelScope(-1))
{
position.xMin -= ReflectedEditorGUI.indent;
}
}

Rect result = EditorGUI.PrefixLabel(position, label, labelStyle);

if (hasChildren || EditorGUI.indentLevel >= 1)
{
result.xMin -= ReflectedEditorGUI.indentWidth;
}

return result;
#elif UNITY_2022_2_OR_NEWER
if (hasChildren || EditorGUI.indentLevel >= 1)
{
position.xMin += ReflectedEditorGUI.indentWidth;
}

GUIStyle labelStyle = isSelected ? Styles.SelectedLabelStyle : Styles.RegularLabelStyle;
Rect result = EditorGUI.PrefixLabel(position, label, labelStyle);
if (hasChildren || EditorGUI.indentLevel>=1)
result.xMin -= ReflectedEditorGUI.indentWidth;
return result;
#else
GUIStyle labelStyle = isSelected ? Styles.SelectedLabelStyle : Styles.RegularLabelStyle;
Rect result = EditorGUI.PrefixLabel(position, label, labelStyle);
return result;
#endif
}

private void DrawObjectField(Rect position, GUIContent objectFieldContent)
Expand Down
11 changes: 9 additions & 2 deletions Editor/Drawers/RawReferenceDrawer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using System;
#if UNITY_2022_3_20 || UNITY_2022_3_21
#define UNITY_2022_3_20_OR_NEWER
#endif

using System;
using System.Reflection;
using TNRD.Utilities;
using UnityEditor;
Expand Down Expand Up @@ -45,7 +49,7 @@ public void OnGUI(Rect position)
? EditorGUIUtility.ObjectContent((MonoScript)null, typeof(MonoScript))
: new GUIContent(rawReferenceValue.GetType().Name, IconUtility.ScriptIcon);

CustomObjectDrawer.OnGUI(objectFieldRect, label, content, Property);
CustomObjectDrawer.OnGUI(objectFieldRect, label, content, Property, rawReferenceValue != null);

HandleDragAndDrop(objectFieldRect);

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

private void DrawProperty(Rect position)
{
#if UNITY_2022_3_20_OR_NEWER
position.xMin -= ReflectedEditorGUI.indent;
#endif
EditorGUI.PropertyField(position,
RawReferenceProperty,
GUIContent.none,
Expand Down
2 changes: 1 addition & 1 deletion Editor/Drawers/UnityReferenceDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void OnGUI(Rect position)
Object unityReference = UnityReferenceProperty.objectReferenceValue;
Type referenceType = unityReference == null ? typeof(Object) : unityReference.GetType();
GUIContent objectContent = EditorGUIUtility.ObjectContent(unityReference, referenceType);
CustomObjectDrawer.OnGUI(position, label, objectContent, Property);
CustomObjectDrawer.OnGUI(position, label, objectContent, Property, false);
HandleDragAndDrop(position);
}

Expand Down
3 changes: 2 additions & 1 deletion Editor/SerializableInterfacePropertyDrawer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using TNRD.Drawers;
using TNRD.Utilities;
using UnityEditor;
using UnityEngine;
using UnityEngine.Assertions;
Expand Down Expand Up @@ -81,7 +82,7 @@ private Type GetGenericArgument()

private IReferenceDrawer GetReferenceDrawer(SerializedProperty property, GUIContent label)
{
SerializedProperty modeProperty = serializedProperty.FindPropertyRelative("mode");
SerializedProperty modeProperty = serializedProperty.ReferenceModeProperty();
ReferenceMode referenceMode = (ReferenceMode)modeProperty.enumValueIndex;

switch (referenceMode)
Expand Down
23 changes: 23 additions & 0 deletions Editor/Utilities/ReflectedEditorGUI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Reflection;
using UnityEditor;

namespace TNRD.Utilities
{
public static class ReflectedEditorGUI
{
private static readonly PropertyInfo indentLevelProperty =
typeof(EditorGUI).GetProperty("indent", BindingFlags.Static | BindingFlags.NonPublic);

private static readonly FieldInfo indentWidthField =
typeof(EditorGUI).GetField("kIndentPerLevel", BindingFlags.Static | BindingFlags.NonPublic);

static ReflectedEditorGUI()
{
indentWidth = (float)indentWidthField.GetValue(null);
}

public static float indent => (float)indentLevelProperty.GetValue(null);

public static float indentWidth { get; private set; }
}
}
3 changes: 3 additions & 0 deletions Editor/Utilities/ReflectedEditorGUI.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c1648be

Please sign in to comment.