Skip to content

Commit 790fbb4

Browse files
committed
New visual appearance for the SerializedDictionary; possibility to use Toolbox drawers within the SerializedDictionary properties
1 parent fa67b73 commit 790fbb4

File tree

7 files changed

+72
-14
lines changed

7 files changed

+72
-14
lines changed

Assets/Editor Toolbox/Editor/Drawers/Toolbox/TargetType/SerializedDictionaryDrawer.cs

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,19 @@ static SerializedDictionaryDrawer()
4646
list.drawElementCallback += (rect, index, isActive, isFocused) =>
4747
{
4848
var element = pairsProperty.GetArrayElementAtIndex(index);
49-
var content = list.GetElementContent(element, index);
49+
var kProperty = element.FindPropertyRelative("key");
50+
var vProperty = element.FindPropertyRelative("value");
5051

51-
using (var propertyScope = new PropertyScope(element, content))
52+
var content = list.GetElementContent(element, index);
53+
using (new EditorGUILayout.HorizontalScope())
5254
{
53-
if (!propertyScope.IsVisible)
54-
{
55-
return;
56-
}
57-
58-
//draw key/value children and use new, shortened labels
59-
EditorGUI.indentLevel++;
60-
EditorGUILayout.PropertyField(element.FindPropertyRelative("key"), new GUIContent("K"));
61-
EditorGUILayout.PropertyField(element.FindPropertyRelative("value"), new GUIContent("V"));
62-
EditorGUI.indentLevel--;
55+
var kOption = GUILayout.Width(Style.kGroupWidth);
56+
DrawDictionaryProperty(kProperty, Style.kLabel, Style.kLabelWidth, kOption);
57+
58+
var vLabel = vProperty.hasVisibleChildren
59+
? Style.vLabel
60+
: GUIContent.none;
61+
DrawDictionaryProperty(vProperty, vLabel, Style.vLabelWidth);
6362
}
6463
};
6564
return list;
@@ -68,6 +67,19 @@ static SerializedDictionaryDrawer()
6867

6968
private static readonly PropertyDataStorage<ReorderableListBase, CreationArgs> storage;
7069

70+
private static void DrawDictionaryProperty(SerializedProperty property, GUIContent label, float labelWidth, params GUILayoutOption[] options)
71+
{
72+
using (new EditorGUILayout.VerticalScope(Style.propertyGroupStyle, options))
73+
{
74+
var indent = property.hasVisibleChildren ? 1 : 0;
75+
using (new ChangeIndentScope(indent))
76+
{
77+
EditorGUIUtility.labelWidth = labelWidth;
78+
ToolboxEditorGui.DrawToolboxProperty(property, label);
79+
EditorGUIUtility.labelWidth = -1;
80+
}
81+
}
82+
}
7183

7284
public override void OnGui(SerializedProperty property, GUIContent label)
7385
{
@@ -92,11 +104,19 @@ public override bool UseForChildren()
92104
return false;
93105
}
94106

95-
96107
private static class Style
97108
{
109+
internal static readonly float kLabelWidth = 40.0f;
110+
internal static readonly float kGroupWidth = 200.0f;
111+
internal static readonly float vLabelWidth = 150.0f;
112+
98113
internal static readonly float warningIconOffset = 25.0f;
99114
internal static readonly string warningMessage = "keys are not unique, it will break deserialization";
115+
116+
internal static readonly GUIContent kLabel = new GUIContent("Key");
117+
internal static readonly GUIContent vLabel = new GUIContent("Value");
118+
119+
internal static readonly GUIStyle propertyGroupStyle = new GUIStyle(EditorStyles.helpBox);
100120
}
101121

102122
private struct CreationArgs
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using UnityEditor;
3+
4+
namespace Toolbox.Editor.Internal
5+
{
6+
internal class ChangeIndentScope : IDisposable
7+
{
8+
private readonly int indentChange;
9+
10+
public ChangeIndentScope(int indentChange)
11+
{
12+
this.indentChange = indentChange;
13+
Prepare(indentChange);
14+
}
15+
16+
public void Prepare(int indentChange)
17+
{
18+
EditorGUI.indentLevel += indentChange;
19+
}
20+
21+
public void Dispose()
22+
{
23+
EditorGUI.indentLevel -= indentChange;
24+
}
25+
}
26+
}

Assets/Editor Toolbox/Editor/Internal/ChangeIndentScope.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/Editor Toolbox/Editor/Internal/ReorderableListBase.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,7 @@ protected static class Style
883883
internal static readonly float handleHeight = 7.0f;
884884
internal static readonly float dragAreaWidth = 40.0f;
885885
internal static readonly float sizeAreaWidth = 19.0f;
886+
internal static readonly float minEmptyHeight = 8.0f;
886887

887888
internal static readonly Color selectionColor = new Color(0.3f, 0.47f, 0.75f);
888889

Assets/Examples/Scenes/SampleScene.unity

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1304,7 +1304,7 @@ MonoBehaviour:
13041304
buildIndex: 0
13051305
dictionary:
13061306
pairs:
1307-
- key: 0
1307+
- key: 2
13081308
value: {fileID: 977748987}
13091309
- key: 1
13101310
value: {fileID: 2037155952}

Docs/dictionary1.png

-2.78 KB
Loading

Docs/dictionary2.png

-403 Bytes
Loading

0 commit comments

Comments
 (0)