Skip to content

Commit 795c87e

Browse files
committed
Improvements to the SerializedScene drawing logic
1 parent 4392c10 commit 795c87e

File tree

2 files changed

+47
-18
lines changed

2 files changed

+47
-18
lines changed

Assets/Editor Toolbox/Editor/Drawers/Regular/PropertyDrawerBase.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ public sealed override void OnGUI(Rect position, SerializedProperty property, GU
4848
}
4949

5050
ToolboxEditorLog.WrongAttributeUsageWarning(attribute, property);
51-
//create additional warning label based on the property name
52-
var warningContent = new GUIContent(property.displayName + " has invalid property drawer");
51+
var warningContent = new GUIContent($"{property.displayName} has invalid property drawer");
5352
ToolboxEditorGui.DrawEmptyProperty(position, property, warningContent);
5453
}
5554

Assets/Editor Toolbox/Editor/Drawers/Regular/SerializedSceneDrawer.cs

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using UnityEditor;
1+
using Toolbox.Editor.Internal;
2+
using UnityEditor;
23
using UnityEngine;
34

45
namespace Toolbox.Editor.Drawers
@@ -28,29 +29,39 @@ private void OpenBuildSettings()
2829
EditorWindow.GetWindow(typeof(BuildPlayerWindow));
2930
}
3031

31-
32-
protected override float GetPropertyHeightSafe(SerializedProperty property, GUIContent label)
33-
{
34-
var lineHeight = EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
35-
return HasSceneDetails(property)
36-
? base.GetPropertyHeightSafe(property, label) + lineHeight * 2
37-
: base.GetPropertyHeightSafe(property, label);
38-
}
39-
4032
protected override void OnGUISafe(Rect position, SerializedProperty property, GUIContent label)
4133
{
34+
var hasDetails = HasSceneDetails(property);
4235
EditorGUI.BeginProperty(position, label, property);
4336
position.height = EditorGUIUtility.singleLineHeight;
44-
position = EditorGUI.PrefixLabel(position, label);
37+
if (hasDetails)
38+
{
39+
position.xMax -= Style.foldoutWidth;
40+
}
41+
4542
var sceneProperty = property.FindPropertyRelative("sceneReference");
46-
EditorGUI.ObjectField(position, sceneProperty, GUIContent.none);
43+
EditorGUI.ObjectField(position, sceneProperty, label);
4744
EditorGUI.EndProperty();
4845

49-
if (!HasSceneDetails(property))
46+
if (hasDetails)
47+
{
48+
var prevXMin = position.xMin;
49+
position.xMin = position.xMax;
50+
position.xMax += Style.foldoutWidth;
51+
using (new DisabledScope(true))
52+
{
53+
property.isExpanded = GUI.Toggle(position, property.isExpanded, Style.foldoutContent, Style.foldoutStyle);
54+
}
55+
56+
position.xMin = prevXMin;
57+
}
58+
59+
if (!hasDetails || !property.isExpanded)
5060
{
5161
return;
5262
}
5363

64+
EditorGUI.indentLevel++;
5465
var sceneData = SceneData.GetSceneDataFromIndex(property);
5566
var spacing = EditorGUIUtility.standardVerticalSpacing;
5667
position.y += EditorGUIUtility.singleLineHeight + spacing;
@@ -64,20 +75,21 @@ protected override void OnGUISafe(Rect position, SerializedProperty property, GU
6475
EditorGUI.LabelField(position, Style.notInBuildContent);
6576
position.y += EditorGUIUtility.singleLineHeight + spacing;
6677
EditorGUI.EndDisabledGroup();
67-
if (GUI.Button(position, Style.showDetailsContent))
78+
var buttonRect = EditorGUI.IndentedRect(position);
79+
if (GUI.Button(buttonRect, Style.showDetailsContent))
6880
{
6981
OpenBuildSettings();
7082
}
7183
}
72-
}
7384

85+
EditorGUI.indentLevel--;
86+
}
7487

7588
public override bool IsPropertyValid(SerializedProperty property)
7689
{
7790
return property.type == nameof(SerializedScene);
7891
}
7992

80-
8193
private struct SceneData
8294
{
8395
public int index;
@@ -134,10 +146,28 @@ public static SceneData GetSceneDataFromScene(SerializedProperty property)
134146

135147
private static class Style
136148
{
149+
internal const float foldoutWidth = 50.0f;
150+
151+
internal static readonly GUIContent foldoutContent = new GUIContent("Details", "Show/Hide Scene Details");
137152
internal static readonly GUIContent buildIndexContent = new GUIContent("Build Index");
138153
internal static readonly GUIContent isEnabledContent = new GUIContent("Is Enabled");
139154
internal static readonly GUIContent notInBuildContent = new GUIContent("Not in Build");
140155
internal static readonly GUIContent showDetailsContent = new GUIContent("Open Build Settings");
156+
157+
internal static readonly GUIStyle foldoutStyle;
158+
159+
static Style()
160+
{
161+
foldoutStyle = new GUIStyle(EditorStyles.miniButton)
162+
{
163+
#if UNITY_2019_3_OR_NEWER
164+
fontSize = 10,
165+
#else
166+
fontSize = 9,
167+
#endif
168+
alignment = TextAnchor.MiddleCenter
169+
};
170+
}
141171
}
142172
}
143173
}

0 commit comments

Comments
 (0)