-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathEditorGUILayoutEx.cs
169 lines (146 loc) · 6.96 KB
/
EditorGUILayoutEx.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using UnityEditor;
using UnityEngine;
namespace cmdwtf.UnityTools.Editor
{
/// <summary>
/// Utilities specific to the <see cref="EditorGUILayout"/> class.
/// </summary>
public static class EditorGUILayoutEx
{
private static Rect? _lastRect;
/// <inheritdoc cref="GUILayoutUtility.GetLastRect"/>
public static Rect GetLastRect()
=> _lastRect ?? Rect.zero;
/// <inheritdoc cref="DropdownButtonLabeled(string,string,UnityEngine.FocusType,UnityEngine.GUIStyle,UnityEngine.GUIStyle,UnityEngine.GUILayoutOption[])"/>
public static bool DropdownButtonLabeled(string label,
string dropdownContent,
FocusType dropdownFocusType,
GUIStyle labelStyle = null,
GUIStyle dropdownStyle = null,
params GUILayoutOption[] layoutOptions
)
=> DropdownButtonLabeled(label.ToGUIContent(), dropdownContent.ToGUIContent(), dropdownFocusType, labelStyle, dropdownStyle, layoutOptions);
/// <inheritdoc cref="DropdownButtonLabeled(string,string,UnityEngine.FocusType,UnityEngine.GUIStyle,UnityEngine.GUIStyle,UnityEngine.GUILayoutOption[])"/>
public static bool DropdownButtonLabeled(string label,
string dropdownContent,
FocusType dropdownFocusType,
params GUILayoutOption[] layoutOptions
)
=> DropdownButtonLabeled(label.ToGUIContent(), dropdownContent.ToGUIContent(), dropdownFocusType, null, null, layoutOptions);
/// <inheritdoc cref="DropdownButtonLabeled(string,string,UnityEngine.FocusType,UnityEngine.GUIStyle,UnityEngine.GUIStyle,UnityEngine.GUILayoutOption[])"/>
public static bool DropdownButtonLabeled(string label,
GUIContent dropdownContent,
FocusType dropdownFocusType,
GUIStyle labelStyle = null,
GUIStyle dropdownStyle = null,
params GUILayoutOption[] layoutOptions
)
=> DropdownButtonLabeled(label.ToGUIContent(), dropdownContent, dropdownFocusType, labelStyle, dropdownStyle, layoutOptions);
/// <inheritdoc cref="DropdownButtonLabeled(string,string,UnityEngine.FocusType,UnityEngine.GUIStyle,UnityEngine.GUIStyle,UnityEngine.GUILayoutOption[])"/>
public static bool DropdownButtonLabeled(string label,
GUIContent dropdownContent,
FocusType dropdownFocusType,
params GUILayoutOption[] layoutOptions
)
=> DropdownButtonLabeled(label.ToGUIContent(), dropdownContent, dropdownFocusType, null, null, layoutOptions);
/// <inheritdoc cref="DropdownButtonLabeled(string,string,UnityEngine.FocusType,UnityEngine.GUIStyle,UnityEngine.GUIStyle,UnityEngine.GUILayoutOption[])"/>
public static bool DropdownButtonLabeled(GUIContent label,
string dropdownContent,
FocusType dropdownFocusType,
GUIStyle labelStyle = null,
GUIStyle dropdownStyle = null,
params GUILayoutOption[] layoutOptions
)
=> DropdownButtonLabeled(label, dropdownContent.ToGUIContent(), dropdownFocusType, labelStyle, dropdownStyle, layoutOptions);
/// <inheritdoc cref="DropdownButtonLabeled(string,string,UnityEngine.FocusType,UnityEngine.GUIStyle,UnityEngine.GUIStyle,UnityEngine.GUILayoutOption[])"/>
public static bool DropdownButtonLabeled(GUIContent label,
string dropdownContent,
FocusType dropdownFocusType,
params GUILayoutOption[] layoutOptions
)
=> DropdownButtonLabeled(label, dropdownContent.ToGUIContent(), dropdownFocusType, null, null, layoutOptions);
/// <inheritdoc cref="DropdownButtonLabeled(string,string,UnityEngine.FocusType,UnityEngine.GUIStyle,UnityEngine.GUIStyle,UnityEngine.GUILayoutOption[])"/>
public static bool DropdownButtonLabeled(GUIContent label,
GUIContent dropdownContent,
FocusType dropdownFocusType,
params GUILayoutOption[] layoutOptions)
=> DropdownButtonLabeled(label, dropdownContent, dropdownFocusType, null, null, layoutOptions);
/// <summary>
/// Draws a dropdown button with a label.
/// </summary>
/// <param name="label">The label text.</param>
/// <param name="dropdownContent">What content should be on the button.</param>
/// <param name="dropdownFocusType">The type of focus the button should use.</param>
/// <param name="labelStyle">The optional style to draw the label with.</param>
/// <param name="dropdownStyle">The optional style to draw the button with.</param>
/// <param name="layoutOptions">Optional layout options.</param>
/// <returns><see langword="true"/>, if the button was clicked.</returns>
public static bool DropdownButtonLabeled(GUIContent label,
GUIContent dropdownContent,
FocusType dropdownFocusType,
GUIStyle labelStyle = null,
GUIStyle dropdownStyle = null,
params GUILayoutOption[] layoutOptions
)
{
// get a 'whole' rect for our control.
Rect rect = EditorGUILayout.GetControlRect(layoutOptions);
// duplicate the main rect, and shrink it
// to the size for for the label.
var labelRect = new Rect(rect) { width = EditorGUIUtility.labelWidth };
// duplicate the main rect, and shrink it
// to the remaining size for the dropdown button, and
// scoot it over the width of the label.
var dropdownRect = new Rect(rect);
dropdownRect.width -= labelRect.width;
dropdownRect.x += labelRect.width;
// draw the label
if (labelStyle != null)
{
EditorGUI.LabelField(labelRect, label, labelStyle);
}
else
{
EditorGUI.LabelField(labelRect, label);
}
// store the last rect for the user to know where the dropdown button was drawn.
_lastRect = dropdownRect;
// draw the dropdown, and return the user clicked value from it.
return dropdownStyle != null
? EditorGUI.DropdownButton(dropdownRect, dropdownContent, dropdownFocusType, dropdownStyle)
: EditorGUI.DropdownButton(dropdownRect, dropdownContent, dropdownFocusType);
}
public static void DropShadowLabel(GUIContent content, GUIStyle style = default)
{
Rect r = EditorGUILayout.GetControlRect();
if (style == default)
{
EditorGUI.DropShadowLabel(r, content);
}
else
{
EditorGUI.DropShadowLabel(r, content, style);
}
_lastRect = r;
}
public static void DropShadowLabel(string text, GUIStyle style = default)
=> DropShadowLabel(new GUIContent(text), style);
public static bool InlineHamburgerMenuButton()
{
GUIStyle hamburgerStyle = new("PaneOptions")
{
padding = GUI.skin.button.padding,
margin = GUI.skin.button.margin,
alignment = TextAnchor.MiddleCenter,
fixedHeight = 0,
imagePosition = ImagePosition.ImageOnly,
stretchHeight = true,
stretchWidth = false,
};
// increase the margin just a smidge so it fits inline better.
hamburgerStyle.margin.top++;
hamburgerStyle.margin.bottom++;
return GUILayout.Button(GUIContent.none, hamburgerStyle);
}
}
}