Skip to content

Commit 5ff4f35

Browse files
committed
GUIContent optimizations
1 parent 2e2d958 commit 5ff4f35

8 files changed

Lines changed: 94 additions & 57 deletions

Editor/EditorGUIExtras.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ namespace UnityEssentialsEditor
1616
{
1717
public static class EditorGUIExtras
1818
{
19+
private static GUIContent content = new GUIContent("");
20+
1921
/// <summary>
2022
/// A monospace font for use in editor GUI elements.
2123
/// </summary>
@@ -231,8 +233,9 @@ private static int HorizontalButtonGroup(Rect position, GUIContent label, int va
231233
GUIStyle style = i == 0 ? EditorStyles.miniButtonLeft : i == count - 1 ? EditorStyles.miniButtonRight : EditorStyles.miniButtonMid;
232234
if(!flags)
233235
{
236+
content.text = names[i];
234237
bool b = values[i] == value;
235-
var b2 = GUI.Toggle(buttonPositions[i], b, names[i], style);
238+
var b2 = GUI.Toggle(buttonPositions[i], b, content, style);
236239
if(b2 && !b)
237240
{
238241
value = values[i];

Editor/Inspector/ExtendedTransformEditor.cs

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class ExtendedTransformEditor : Editor
1616

1717
private Editor _defaultEditor;
1818
private Transform _transform;
19+
1920
private static bool expandExtraProperties = false;
2021
private static bool expandExtraTools = false;
2122

@@ -27,6 +28,27 @@ public class ExtendedTransformEditor : Editor
2728
private static Vector3? copiedPosition;
2829
private static Quaternion? copiedRotation;
2930
private static Vector3? copiedScale;
31+
32+
private static readonly GUIContent reset = new GUIContent("Reset");
33+
private static readonly GUIContent apply = new GUIContent("Apply");
34+
private static readonly GUIContent position = new GUIContent("Position");
35+
private static readonly GUIContent rotation = new GUIContent("Rotation");
36+
private static readonly GUIContent scale = new GUIContent("Scale");
37+
private static readonly GUIContent fullTransform = new GUIContent("Full Transform");
38+
private static readonly GUIContent worldPosition = new GUIContent("Position (world)");
39+
private static readonly GUIContent worldRotation = new GUIContent("Rotation (world)");
40+
private static readonly GUIContent lossyScale = new GUIContent("Scale (lossy)");
41+
private static readonly GUIContent upDirection = new GUIContent("Up Direction");
42+
private static readonly GUIContent forwardDirection = new GUIContent("Forward Direction");
43+
private static readonly GUIContent recursiveChildCount = new GUIContent("Child Count (recursive)");
44+
private static readonly GUIContent parentDepth = new GUIContent("Parent Depth");
45+
private static readonly GUIContent copy = new GUIContent("Copy");
46+
private static readonly GUIContent align = new GUIContent("Align");
47+
private static readonly GUIContent paste = new GUIContent("Paste");
48+
private static readonly GUIContent hierarchyPath = new GUIContent("Hierarchy Path");
49+
private static GUIContent hierarchyPathString = new GUIContent("");
50+
private static GUIContent childCounter = new GUIContent("");
51+
private static GUIContent parentCounter = new GUIContent("");
3052

3153
private void OnEnable()
3254
{
@@ -89,33 +111,36 @@ private void DrawExtraProperties(bool foldout)
89111
if(targets.Length == 1)
90112
{
91113
EditorGUI.BeginChangeCheck();
92-
var worldPos = EditorGUILayout.Vector3Field("Position (world)", _transform.position);
114+
var worldPos = EditorGUILayout.Vector3Field(worldPosition, _transform.position);
93115
if(EditorGUI.EndChangeCheck())
94116
{
95117
Undo.RecordObject(_transform, "Move (world)");
96118
_transform.position = worldPos;
97119
}
98120
EditorGUI.BeginChangeCheck();
99-
var worldEuler = EditorGUILayout.Vector3Field("Rotation (world)", _transform.eulerAngles);
121+
var worldEuler = EditorGUILayout.Vector3Field(worldRotation, _transform.eulerAngles);
100122
if(EditorGUI.EndChangeCheck())
101123
{
102124
Undo.RecordObject(_transform, "Rotate (world)");
103125
_transform.eulerAngles = worldEuler;
104126
}
105127
GUI.backgroundColor = Color.white.WithAlpha(0.5f);
106-
EditorGUILayout.Vector3Field("Scale (lossy)", _transform.lossyScale);
128+
EditorGUILayout.Vector3Field(lossyScale, _transform.lossyScale);
107129
EditorGUILayout.Space();
108130
EditorGUI.BeginChangeCheck();
109-
EditorGUILayout.Vector3Field("Up Direction", _transform.up);
110-
EditorGUILayout.Vector3Field("Forward Direction", _transform.forward);
131+
EditorGUILayout.Vector3Field(upDirection, _transform.up);
132+
EditorGUILayout.Vector3Field(forwardDirection, _transform.forward);
111133
GUI.backgroundColor = Color.white;
112134
EditorGUILayout.Space();
113-
EditorGUILayout.LabelField("Child Count (recursive)", $"{_transform.childCount} ({RecursiveChildCount(_transform)})");
114-
EditorGUILayout.LabelField("Parent Depth", RecursiveParentCount(_transform).ToString());
135+
childCounter.text = $"{_transform.childCount} ({RecursiveChildCount(_transform)})";
136+
EditorGUILayout.LabelField(recursiveChildCount, childCounter);
137+
parentCounter.text = RecursiveParentCount(_transform).ToString();
138+
EditorGUILayout.LabelField(parentDepth, parentCounter);
115139
GUILayout.BeginHorizontal();
116-
GUILayout.Label("Hierarchy Path", GUILayout.Width(EditorGUIUtility.labelWidth - 3));
140+
GUILayout.Label(hierarchyPath, GUILayout.Width(EditorGUIUtility.labelWidth - 3));
117141

118-
GUILayout.Label(_transform.GetHierarchyPathString(), pathLabelStyle, GUILayout.Width(EditorGUIUtility.currentViewWidth - EditorGUIUtility.labelWidth - 30));
142+
hierarchyPathString.text = _transform.GetHierarchyPathString();
143+
GUILayout.Label(hierarchyPathString, pathLabelStyle, GUILayout.Width(EditorGUIUtility.currentViewWidth - EditorGUIUtility.labelWidth - 30));
119144
GUILayout.EndHorizontal();
120145
}
121146
else
@@ -138,7 +163,7 @@ private void DrawToolbars(bool foldout)
138163
private void DrawPrimaryToolbar()
139164
{
140165
EditorGUILayout.BeginHorizontal(EditorStyles.toolbar);
141-
TriButtonRow(new GUIContent("Reset"), TOOLBAR_BUTTON_WIDTH * 2,
166+
TriButtonRow(reset, TOOLBAR_BUTTON_WIDTH * 2,
142167
t =>
143168
{
144169
Undo.RecordObject(t, "Reset Transform");
@@ -165,13 +190,13 @@ private void DrawPrimaryToolbar()
165190

166191
GUI.enabled = _transform.childCount > 0;
167192
var rect = GUILayoutUtility.GetRect(TOOLBAR_BUTTON_WIDTH * 2, 0, GUILayout.ExpandHeight(true));
168-
if(EditorGUI.DropdownButton(rect, new GUIContent("Apply"), FocusType.Passive, EditorStyles.toolbarDropDown))
193+
if(EditorGUI.DropdownButton(rect, apply, FocusType.Passive, EditorStyles.toolbarDropDown))
169194
{
170195
var menu = new GenericMenu();
171-
menu.AddItem(new GUIContent("Position"), false, () => ExtraContextMenuItems.ApplyPosition(new MenuCommand(_transform)));
172-
menu.AddItem(new GUIContent("Rotation"), false, () => ExtraContextMenuItems.ApplyRotation(new MenuCommand(_transform)));
173-
menu.AddItem(new GUIContent("Scale"), false, () => ExtraContextMenuItems.ApplyScale(new MenuCommand(_transform)));
174-
menu.AddItem(new GUIContent("Full Transform"), false, () => ExtraContextMenuItems.ApplyFullTransform(new MenuCommand(_transform)));
196+
menu.AddItem(position, false, () => ExtraContextMenuItems.ApplyPosition(new MenuCommand(_transform)));
197+
menu.AddItem(rotation, false, () => ExtraContextMenuItems.ApplyRotation(new MenuCommand(_transform)));
198+
menu.AddItem(scale, false, () => ExtraContextMenuItems.ApplyScale(new MenuCommand(_transform)));
199+
menu.AddItem(fullTransform, false, () => ExtraContextMenuItems.ApplyFullTransform(new MenuCommand(_transform)));
175200
menu.DropDown(rect);
176201
}
177202
GUI.enabled = true;
@@ -180,34 +205,34 @@ private void DrawPrimaryToolbar()
180205

181206
//Copy transform data
182207
GUI.enabled = targets.Length == 1;
183-
TriButtonRow(new GUIContent("Copy"), TOOLBAR_BUTTON_WIDTH * 2,
208+
TriButtonRow(copy, TOOLBAR_BUTTON_WIDTH * 2,
184209
t =>
185210
{
186211
//Copy all transform data
187-
copiedPosition = _transform.position;
188-
copiedRotation = _transform.rotation;
189-
copiedScale = _transform.localScale;
212+
copiedPosition = t.position;
213+
copiedRotation = t.rotation;
214+
copiedScale = t.localScale;
190215
},
191216
t =>
192217
{
193218
//Copy position
194-
copiedPosition = _transform.position;
219+
copiedPosition = t.position;
195220
copiedRotation = null;
196221
copiedScale = null;
197222
},
198223
t =>
199224
{
200225
//Copy rotation
201226
copiedPosition = null;
202-
copiedRotation = _transform.rotation;
227+
copiedRotation = t.rotation;
203228
copiedScale = null;
204229
},
205230
t =>
206231
{
207232
//Copy scale
208233
copiedPosition = null;
209234
copiedRotation = null;
210-
copiedScale = _transform.localScale;
235+
copiedScale = t.localScale;
211236
}
212237
);
213238
GUI.enabled = true;
@@ -219,7 +244,7 @@ private void DrawSecondaryToolbar()
219244
{
220245
GUILayout.BeginHorizontal(EditorStyles.toolbar);
221246

222-
TriButtonRow(new GUIContent("Align"), TOOLBAR_BUTTON_WIDTH * 2,
247+
TriButtonRow(align, TOOLBAR_BUTTON_WIDTH * 2,
223248
t =>
224249
{
225250
Undo.RecordObject(t, "Align Transform to View");
@@ -242,7 +267,7 @@ private void DrawSecondaryToolbar()
242267
GUILayout.FlexibleSpace();
243268

244269
//Paste transform data
245-
TriButtonRow(new GUIContent("Paste"), TOOLBAR_BUTTON_WIDTH * 2,
270+
TriButtonRow(paste, TOOLBAR_BUTTON_WIDTH * 2,
246271
t =>
247272
{
248273
Undo.RecordObject(t, "Paste Transform Values");

Editor/PropertyDrawers/ButtonAttributeDrawer.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ private static void DrawButtons(SerializedProperty property, ButtonAttribute att
4242
var rects = position.DivideHorizontal(attribute.methodNames.Length, 4);
4343
for(int i = 0; i < rects.Length; i++)
4444
{
45-
string name = attribute.labels[i] ?? ObjectNames.NicifyVariableName(attribute.methodNames[i]);
46-
if(GUI.Button(rects[i], name))
45+
if(GUI.Button(rects[i], attribute.labels[i]))
4746
{
4847
Invoke(property, attribute.methodNames[i], attribute.arguments[i]);
4948
}

Editor/PropertyDrawers/CharRestrictionAttributeDrawer.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public class CharRestrictionAttributeDrawer : PropertyDrawer
1111
{
1212
private static Texture infoIcon;
1313

14+
private static GUIContent label = new GUIContent();
15+
1416
public override void OnGUI(Rect position, SerializedProperty property, GUIContent content)
1517
{
1618

@@ -41,14 +43,14 @@ public static void DrawRestrictedTextField(Rect position, SerializedProperty pro
4143
if(restricted)
4244
{
4345
GUI.color = GUI.color.MultiplyAlpha(0.25f);
44-
GUIContent info = new GUIContent();
45-
info.tooltip = "The following characters are allowed:\n" + allowedChars;
46+
label = new GUIContent();
47+
label.tooltip = "The following characters are allowed:\n" + allowedChars;
4648
if(forcedCase.HasValue)
4749
{
48-
info.tooltip += "\n" + (forcedCase.Value ? "Uppercase" : "Lowercase") + " only.";
50+
label.tooltip += "\n" + (forcedCase.Value ? "Uppercase" : "Lowercase") + " only.";
4951
}
5052
GUI.DrawTexture(infoRect, infoIcon, ScaleMode.ScaleToFit);
51-
GUI.Label(infoRect, info);
53+
GUI.Label(infoRect, label);
5254
GUI.color = GUI.color.MultiplyAlpha(4.0f);
5355
value = ApplyRestrictions(value, allowedChars, forcedCase, replacementChar);
5456
}

Editor/PropertyDrawers/ComponentStackDrawer.cs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ public class ComponentStackDrawer : PropertyDrawer
1818
private Dictionary<Type, string> supportedTypes;
1919
private SerializedObject serializedObject;
2020

21-
static Dictionary<Type, Dictionary<Type, string>> subtypes = new Dictionary<Type, Dictionary<Type, string>>();
21+
static Dictionary<Type, Dictionary<Type, string>> subtypes = new();
2222

2323
static GUIStyle headerStyle;
24+
private static GUIContent expandAll = new GUIContent("Expand All");
25+
private static GUIContent collapseAll = new GUIContent("Collapse All");
26+
private static GUIContent title = new GUIContent();
2427

2528
static string jsonClipboard;
2629
static Type jsonClipboardType;
@@ -33,8 +36,8 @@ public class ComponentStackDrawer : PropertyDrawer
3336
private static bool needsRenameFocus;
3437
private static string renameInput;
3538

36-
private static Dictionary<Type, Type> drawerTypes = new Dictionary<Type, Type>();
37-
private List<StackElementDrawer> drawerInstances = new List<StackElementDrawer>();
39+
private static Dictionary<Type, Type> drawerTypes = new();
40+
private List<StackElementDrawer> drawerInstances = new();
3841

3942
private static void Init()
4043
{
@@ -64,7 +67,7 @@ private void GetSupportedTypes(Type elementBaseType)
6467
{
6568
//Find all valid subtypes for this type
6669
var types = new List<Type>(ReflectionUtility.GetGameAssembliesIncludingUnity().SelectMany(a => a.GetTypes().Where(t => elementBaseType.IsAssignableFrom(t) && !t.IsAbstract)));
67-
supportedTypes = new Dictionary<Type, string>();
70+
supportedTypes = new();
6871
foreach(var t in types)
6972
{
7073
string menuName = "";
@@ -107,7 +110,7 @@ public override void OnGUI(Rect position, SerializedProperty stackProp, GUIConte
107110
GetSupportedTypes(stackElemType);
108111
}
109112

110-
if(headerStyle == null) headerStyle = new GUIStyle(EditorStyles.toolbar) { fixedHeight = 0 };
113+
if(headerStyle == null) headerStyle = new(EditorStyles.toolbar) { fixedHeight = 0 };
111114
GUIStyle boxStyle = "FrameBox";
112115
GUIStyle topBoxStyle = "Tab onlyOne";
113116

@@ -129,8 +132,8 @@ public override void OnGUI(Rect position, SerializedProperty stackProp, GUIConte
129132
{
130133
var menu = new GenericMenu();
131134
var stackPath = arrayProp.propertyPath;
132-
menu.AddItem(new GUIContent("Expand All"), false, () => ToggleExpandedStateAll(arrayProp.serializedObject, stackPath, true));
133-
menu.AddItem(new GUIContent("Collapse All"), false, () => ToggleExpandedStateAll(arrayProp.serializedObject, stackPath, false));
135+
menu.AddItem(expandAll, false, () => ToggleExpandedStateAll(arrayProp.serializedObject, stackPath, true));
136+
menu.AddItem(collapseAll, false, () => ToggleExpandedStateAll(arrayProp.serializedObject, stackPath, false));
134137
/*
135138
menu.AddSeparator("");
136139
menu.AddItem(new GUIContent("Copy Stack"), false, () => CopyObject(obj));
@@ -248,8 +251,8 @@ private float DrawItem(Rect position, SerializedProperty arrayProp, SerializedPr
248251

249252
internal static void DrawItemHeader(Rect position, int i, SerializedProperty prop, StackComponent obj, SerializedProperty array)
250253
{
251-
EditorGUI.BeginProperty(position, new GUIContent(prop.displayName), prop);
252-
var title = new GUIContent(obj != null ? obj.HeaderTitle : "null");
254+
EditorGUI.BeginProperty(position, new(prop.displayName), prop);
255+
title.text = obj != null ? obj.HeaderTitle : "null";
253256

254257
var headerRect = position;
255258
headerRect.xMin++;
@@ -283,7 +286,7 @@ internal static void DrawItemHeader(Rect position, int i, SerializedProperty pro
283286
var menu = new GenericMenu();
284287
if(obj != null)
285288
{
286-
menu.AddItem(new GUIContent("Copy Element"), false, () => CopyObject(obj));
289+
menu.AddItem(new("Copy Element"), false, () => CopyObject(obj));
287290
bool canPaste = !string.IsNullOrEmpty(jsonClipboard) && obj.GetType() == jsonClipboardType;
288291
var paste = new GUIContent("Paste Element Values");
289292
if(canPaste)
@@ -295,7 +298,7 @@ internal static void DrawItemHeader(Rect position, int i, SerializedProperty pro
295298
menu.AddDisabledItem(paste, false);
296299
}
297300
}
298-
menu.AddItem(new GUIContent("Remove Element"), false, () =>
301+
menu.AddItem(new("Remove Element"), false, () =>
299302
{
300303
Undo.RecordObject(array.serializedObject.targetObject, "Remove " + title);
301304
RemoveItem((IList)array.GetValue(), i);
@@ -329,7 +332,7 @@ internal static void DrawItemHeader(Rect position, int i, SerializedProperty pro
329332
}
330333

331334
menu.AddSeparator("");
332-
menu.AddItem(new GUIContent("Set Custom Name"), false, () =>
335+
menu.AddItem(new("Set Custom Name"), false, () =>
333336
{
334337
renamingElement = obj;
335338
needsRenameFocus = true;
@@ -409,7 +412,7 @@ private void DrawFooter(Rect position, SerializedProperty stack, GUIStyle style)
409412
var menu = new GenericMenu();
410413
foreach(var t in supportedTypes)
411414
{
412-
menu.AddItem(new GUIContent(t.Value), false, () =>
415+
menu.AddItem(new(t.Value), false, () =>
413416
{
414417
Undo.RecordObject(serializedObject.targetObject, "Add " + t.Key.Name);
415418
AddNewItem(stack, t.Key);

Editor/PropertyDrawers/CreateAssetButtonAttributeDrawer.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ namespace UnityEssentialsEditor.PropertyDrawers
88
[CustomPropertyDrawer(typeof(CreateAssetButtonAttribute))]
99
public class CreateAssetButtonAttributeDrawer : PropertyDrawer
1010
{
11+
private static readonly GUIContent buttonContent = new GUIContent("New", "Create a new asset and assign it to this field.");
12+
1113
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
1214
{
1315
var type = PropertyDrawerUtility.GetPropertyType(property);
@@ -17,7 +19,7 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
1719
property.objectReferenceValue = EditorGUI.ObjectField(position, label, property.objectReferenceValue, type, false);
1820
position.x += position.width + 5;
1921
position.width = 40;
20-
if(GUI.Button(position, "New"))
22+
if(GUI.Button(position, buttonContent))
2123
{
2224
CreateNewAssetDialog(property.serializedObject, property.propertyPath, type, ext);
2325
GUIUtility.ExitGUI();

Editor/PropertyDrawers/CustomElementDrawerAttribute.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)