Skip to content

Commit 89ab2f7

Browse files
committed
Add code cleanup
1 parent 37bbe55 commit 89ab2f7

File tree

4 files changed

+129
-134
lines changed

4 files changed

+129
-134
lines changed

Editor/FolderEditor.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
using UnityEngine;
33
using UnityHierarchyFolders.Runtime;
44

5-
namespace UnityHierarchyFolders.Editor {
5+
namespace UnityHierarchyFolders.Editor
6+
{
67
[CustomEditor(typeof(Folder))]
78
public class FolderEditor : UnityEditor.Editor
89
{
@@ -11,18 +12,18 @@ public class FolderEditor : UnityEditor.Editor
1112
public override bool RequiresConstantRepaint() => true;
1213
public override void OnInspectorGUI()
1314
{
14-
_expanded = EditorGUILayout.Foldout(_expanded, "Icon Color");
15-
if (_expanded) { RenderColorPicker(); }
15+
this._expanded = EditorGUILayout.Foldout(this._expanded, "Icon Color");
16+
if (this._expanded) { this.RenderColorPicker(); }
1617
}
1718

1819
private void RenderColorPicker()
1920
{
20-
SerializedProperty colorIndexProperty = serializedObject.FindProperty("_colorIndex");
21+
var colorIndexProperty = this.serializedObject.FindProperty("_colorIndex");
2122

2223
EditorGUILayout.BeginHorizontal();
2324
GUILayout.FlexibleSpace();
2425

25-
var buttonSize = 25f;
26+
float buttonSize = 25f;
2627

2728
var gridRect = EditorGUILayout.GetControlRect(false, buttonSize * HierarchyFolderIcon.IconRowCount,
2829
GUILayout.Width(buttonSize * HierarchyFolderIcon.IconColumnCount));
@@ -36,7 +37,7 @@ private void RenderColorPicker()
3637
float width = gridRect.width / HierarchyFolderIcon.IconColumnCount;
3738
float height = gridRect.height / HierarchyFolderIcon.IconRowCount;
3839
var rect = new Rect(gridRect.x + width * column, gridRect.y + height * row, width, height);
39-
var (openIcon, closeIcon) = HierarchyFolderIcon.ColoredFolderIcons(index);
40+
(var openIcon, var closeIcon) = HierarchyFolderIcon.ColoredFolderIcons(index);
4041

4142
if (Event.current.type == EventType.Repaint)
4243
{
@@ -56,9 +57,9 @@ private void RenderColorPicker()
5657

5758
if (GUI.Button(rect, currentIndex == index ? openIcon : closeIcon, EditorStyles.label))
5859
{
59-
Undo.RecordObject(target, "Set Folder Color");
60+
Undo.RecordObject(this.target, "Set Folder Color");
6061
colorIndexProperty.intValue = currentIndex == index ? 0 : index;
61-
serializedObject.ApplyModifiedProperties();
62+
this.serializedObject.ApplyModifiedProperties();
6263
EditorApplication.RepaintHierarchyWindow();
6364
GUIUtility.ExitGUI();
6465
}

Editor/FolderEditorUtils.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,24 @@ namespace UnityHierarchyFolders.Editor
99
{
1010
public static class FolderEditorUtils
1111
{
12-
const string actionName = "Create Heirarchy Folder %#&N";
12+
private const string _actionName = "Create Heirarchy Folder %#&N";
1313

1414
/// <summary>Add new folder "prefab".</summary>
1515
/// <param name="command">Menu command information.</param>
16-
[MenuItem("GameObject/" + actionName, isValidateFunction: false, priority: 0)]
16+
[MenuItem("GameObject/" + _actionName, isValidateFunction: false, priority: 0)]
1717
public static void AddFolderPrefab(MenuCommand command)
1818
{
1919
var obj = new GameObject { name = "Folder" };
2020
obj.AddComponent<Folder>();
2121

2222
GameObjectUtility.SetParentAndAlign(obj, (GameObject)command.context);
23-
Undo.RegisterCreatedObjectUndo(obj, actionName);
23+
Undo.RegisterCreatedObjectUndo(obj, _actionName);
2424
}
2525
}
2626

2727
public class FolderOnBuild : IProcessSceneWithReport
2828
{
29-
public int callbackOrder { get => 0; }
29+
public int callbackOrder => 0;
3030

3131
public void OnProcessScene(Scene scene, BuildReport report)
3232
{

Editor/HierarchyFolderIcon.cs

Lines changed: 87 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,43 @@ namespace UnityHierarchyFolders.Editor
1515
public class HierarchyFolderIcon
1616
{
1717
#if UNITY_2020_1_OR_NEWER
18-
private const string OpenedFolderPrefix = "FolderOpened";
18+
private const string _openedFolderPrefix = "FolderOpened";
1919
#else
20-
private const string OpenedFolderPrefix = "OpenedFolder";
20+
private const string _openedFolderPrefix = "OpenedFolder";
2121
#endif
22-
private const string ClosedFolderPrefix = "Folder";
23-
24-
private static Texture2D openFolderTexture;
25-
private static Texture2D closedFolderTexture;
26-
private static Texture2D openFolderSelectedTexture;
27-
private static Texture2D closedFolderSelectedTexture;
28-
29-
private static bool isInitialized;
30-
private static bool hasProcessedFrame = true;
31-
22+
private const string _closedFolderPrefix = "Folder";
23+
24+
private static Texture2D _openFolderTexture;
25+
private static Texture2D _closedFolderTexture;
26+
private static Texture2D _openFolderSelectedTexture;
27+
private static Texture2D _closedFolderSelectedTexture;
28+
29+
private static bool _isInitialized;
30+
private static bool _hasProcessedFrame = true;
31+
32+
3233
// Reflected members
34+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "Special naming scheme")]
3335
private static PropertyInfo prop_sceneHierarchy;
36+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "Special naming scheme")]
3437
private static PropertyInfo prop_treeView;
38+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "Special naming scheme")]
3539
private static PropertyInfo prop_data;
40+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "Special naming scheme")]
3641
private static PropertyInfo prop_selectedIcon;
42+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "Special naming scheme")]
3743
private static PropertyInfo prop_objectPPTR;
3844

45+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "Special naming scheme")]
3946
private static MethodInfo meth_getRows;
47+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "Special naming scheme")]
4048
private static MethodInfo meth_isExpanded;
49+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "Special naming scheme")]
4150
private static MethodInfo meth_getAllSceneHierarchyWindows;
4251

4352
private static (Texture2D open, Texture2D closed)[] _coloredFolderIcons;
4453
public static (Texture2D open, Texture2D closed) ColoredFolderIcons(int i) => _coloredFolderIcons[i];
45-
54+
4655
public static int IconColumnCount => IconColors.GetLength(0);
4756
public static int IconRowCount => IconColors.GetLength(1);
4857

@@ -57,103 +66,87 @@ public class HierarchyFolderIcon
5766
{new Color(0.91f, 0.30f, 0.24f), new Color(0.77f, 0.15f, 0.09f),},
5867
{new Color(0.35f, 0.49f, 0.63f), new Color(0.24f, 0.33f, 0.42f),},
5968
};
60-
61-
[InitializeOnLoadMethod]
62-
static void Startup()
63-
{
69+
70+
[InitializeOnLoadMethod]
71+
private static void Startup()
72+
{
6473
EditorApplication.update += ResetFolderIcons;
6574
EditorApplication.hierarchyWindowItemOnGUI += RefreshFolderIcons;
6675
}
6776

68-
private static Texture2D GetTintedTexture(Texture2D original, Color tint, string name = "")
69-
{
70-
Color32 TintColor(Color32 c)
71-
{
72-
return c * tint;
73-
}
74-
75-
return GetColorizedTexture(original, TintColor, name);
76-
}
77-
78-
private static Texture2D GetWhiteTexture(Texture2D original, string name = "")
79-
{
80-
Color32 MakeColorsWhite(Color32 c)
81-
{
82-
byte a = c.a;
83-
c = Color.HSVToRGB(0, 0, 1);
84-
c.a = a;
85-
86-
return c;
87-
}
77+
private static Texture2D GetTintedTexture(Texture2D original, Color tint, string name = "") =>
78+
GetColorizedTexture(original, c => c * tint, name);
8879

89-
return GetColorizedTexture(original, MakeColorsWhite, name);
90-
}
80+
private static Texture2D GetWhiteTexture(Texture2D original, string name = "") =>
81+
GetColorizedTexture(original, c => new Color(255, 255, 255, c.a), name);
9182

92-
private static Texture2D GetColorizedTexture(Texture2D original, Func<Color32, Color32> colorManipulator, string name = "")
83+
private static Texture2D GetColorizedTexture(Texture2D original, Func<Color32, Color32> colorFilter, string name = "")
9384
{
94-
var tinted = new Texture2D(original.width, original.height,
95-
original.graphicsFormat, original.mipmapCount, TextureCreationFlags.MipChain);
85+
var tinted = new Texture2D(original.width, original.height,
86+
original.graphicsFormat, original.mipmapCount, TextureCreationFlags.MipChain)
87+
{
88+
name = name
89+
};
9690

97-
tinted.name = name;
98-
9991
Graphics.CopyTexture(original, tinted);
100-
92+
10193
var data = tinted.GetRawTextureData<Color32>();
10294
for (int index = 0, len = data.Length; index < len; index++)
10395
{
104-
data[index] = colorManipulator(data[index]);
96+
data[index] = colorFilter(data[index]);
10597
}
10698

107-
var mipmapSize = tinted.width * tinted.height * 4;
108-
var offset = 0;
99+
int mipmapSize = tinted.width * tinted.height * 4;
100+
int offset = 0;
109101
for (int index = 0; index < tinted.mipmapCount; index++)
110102
{
111103
tinted.SetPixelData(data, index, offset);
112104
mipmapSize >>= 2;
113105
offset += mipmapSize;
114106
}
115-
107+
116108
tinted.hideFlags = HideFlags.DontSave;
117109
tinted.Apply();
118-
110+
119111
return tinted;
120112
}
121-
113+
122114
private static void InitIfNeeded()
123115
{
124-
if (isInitialized) { return; }
125-
126-
openFolderTexture = (Texture2D) EditorGUIUtility.IconContent($"{OpenedFolderPrefix} Icon").image;
127-
closedFolderTexture = (Texture2D) EditorGUIUtility.IconContent($"{ClosedFolderPrefix} Icon").image;
128-
129-
// We could use the actual white folder icons but I prefer the look of the tinted folder icon
130-
// so I'm leaving this as a documented alternative.
131-
//openFolderSelectedTexture = (Texture2D) EditorGUIUtility.IconContent($"{OpenedFolderPrefix} On Icon").image;
132-
//closedFolderSelectedTexture = (Texture2D) EditorGUIUtility.IconContent($"{ClosedFolderPrefix} On Icon").image;
133-
openFolderSelectedTexture = GetWhiteTexture(openFolderTexture, $"{OpenedFolderPrefix} Icon White");
134-
closedFolderSelectedTexture = GetWhiteTexture(closedFolderTexture, $"{ClosedFolderPrefix} Icon White");
135-
136-
_coloredFolderIcons = new (Texture2D, Texture2D)[] {(openFolderTexture, closedFolderTexture)};
137-
116+
if (_isInitialized) { return; }
117+
118+
_openFolderTexture = (Texture2D)EditorGUIUtility.IconContent($"{_openedFolderPrefix} Icon").image;
119+
_closedFolderTexture = (Texture2D)EditorGUIUtility.IconContent($"{_closedFolderPrefix} Icon").image;
120+
121+
// We could use the actual white folder icons but I prefer the look of the tinted white folder icon
122+
// To use the actual white version:
123+
// texture = (Texture2D) EditorGUIUtility.IconContent($"{OpenedFolderPrefix | ClosedFolderPrefix} On Icon").image;
124+
_openFolderSelectedTexture = GetWhiteTexture(_openFolderTexture, $"{_openedFolderPrefix} Icon White");
125+
_closedFolderSelectedTexture = GetWhiteTexture(_closedFolderTexture, $"{_closedFolderPrefix} Icon White");
126+
127+
_coloredFolderIcons = new (Texture2D, Texture2D)[] { (_openFolderTexture, _closedFolderTexture) };
128+
138129
for (int row = 0; row < IconRowCount; row++)
139130
{
140131
for (int column = 0; column < IconColumnCount; column++)
141132
{
142133
int index = 1 + column + row * IconColumnCount;
143134
var color = IconColors[column, row];
144-
145-
var openFolderIcon = GetTintedTexture(openFolderSelectedTexture,
146-
color, $"{openFolderSelectedTexture.name} {index}");
147-
var closedFolderIcon = GetTintedTexture(closedFolderSelectedTexture,
148-
color, $"{closedFolderSelectedTexture.name} {index}");
149-
135+
136+
var openFolderIcon = GetTintedTexture(_openFolderSelectedTexture,
137+
color, $"{_openFolderSelectedTexture.name} {index}");
138+
var closedFolderIcon = GetTintedTexture(_closedFolderSelectedTexture,
139+
color, $"{_closedFolderSelectedTexture.name} {index}");
140+
150141
ArrayUtility.Add(ref _coloredFolderIcons, (openFolderIcon, closedFolderIcon));
151142
}
152143
}
153-
154-
const BindingFlags BindingAll = BindingFlags.Public
144+
145+
// reflection
146+
147+
const BindingFlags BindingAll = BindingFlags.Public
155148
| BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance;
156-
149+
157150
var assembly = typeof(SceneView).Assembly;
158151

159152
var type_sceneHierarchyWindow = assembly.GetType("UnityEditor.SceneHierarchyWindow");
@@ -162,52 +155,52 @@ private static void InitIfNeeded()
162155

163156
var type_sceneHierarchy = assembly.GetType("UnityEditor.SceneHierarchy");
164157
prop_treeView = type_sceneHierarchy.GetProperty("treeView", BindingAll);
165-
158+
166159
var type_treeViewController = assembly.GetType("UnityEditor.IMGUI.Controls.TreeViewController");
167160
prop_data = type_treeViewController.GetProperty("data", BindingAll);
168161

169162
var type_iTreeViewDataSource = assembly.GetType("UnityEditor.IMGUI.Controls.ITreeViewDataSource");
170163
meth_getRows = type_iTreeViewDataSource.GetMethod("GetRows");
171-
meth_isExpanded = type_iTreeViewDataSource.GetMethod("IsExpanded", new Type[] {typeof(TreeViewItem)});
172-
164+
meth_isExpanded = type_iTreeViewDataSource.GetMethod("IsExpanded", new Type[] { typeof(TreeViewItem) });
165+
173166
var type_gameObjectTreeViewItem = assembly.GetType("UnityEditor.GameObjectTreeViewItem");
174167
prop_selectedIcon = type_gameObjectTreeViewItem.GetProperty("selectedIcon", BindingAll);
175168
prop_objectPPTR = type_gameObjectTreeViewItem.GetProperty("objectPPTR", BindingAll);
176169

177-
isInitialized = true;
170+
_isInitialized = true;
178171
}
179172

180173
private static void ResetFolderIcons()
181174
{
182175
InitIfNeeded();
183-
hasProcessedFrame = false;
176+
_hasProcessedFrame = false;
184177
}
185178

186179
private static void RefreshFolderIcons(int instanceid, Rect selectionrect)
187180
{
188-
if (hasProcessedFrame) { return; }
189-
190-
hasProcessedFrame = true;
191-
181+
if (_hasProcessedFrame) { return; }
182+
183+
_hasProcessedFrame = true;
184+
192185
var windows = ((IEnumerable)meth_getAllSceneHierarchyWindows.Invoke(null, Array.Empty<object>())).Cast<EditorWindow>().ToList();
193-
foreach (EditorWindow window in windows)
186+
foreach (var window in windows)
194187
{
195-
var sceneHierarchy = prop_sceneHierarchy.GetValue(window);
196-
var treeView = prop_treeView.GetValue(sceneHierarchy);
197-
var data = prop_data.GetValue(treeView);
188+
object sceneHierarchy = prop_sceneHierarchy.GetValue(window);
189+
object treeView = prop_treeView.GetValue(sceneHierarchy);
190+
object data = prop_data.GetValue(treeView);
198191

199-
var rows = (IList<TreeViewItem>) meth_getRows.Invoke(data, Array.Empty<object>());
200-
foreach (TreeViewItem item in rows)
192+
var rows = (IList<TreeViewItem>)meth_getRows.Invoke(data, Array.Empty<object>());
193+
foreach (var item in rows)
201194
{
202-
var itemObject = (Object) prop_objectPPTR.GetValue(item);
195+
var itemObject = (Object)prop_objectPPTR.GetValue(item);
203196
if (!Folder.TryGetIconIndex(itemObject, out int colorIndex)) { continue; }
204197

205-
var isExpanded = (bool) meth_isExpanded.Invoke(data, new object[] { item });
206-
198+
bool isExpanded = (bool)meth_isExpanded.Invoke(data, new object[] { item });
199+
207200
var icons = ColoredFolderIcons(Mathf.Clamp(colorIndex, 0, _coloredFolderIcons.Length - 1));
208201
item.icon = isExpanded ? icons.open : icons.closed;
209-
210-
prop_selectedIcon.SetValue(item, isExpanded ? openFolderSelectedTexture : closedFolderSelectedTexture);
202+
203+
prop_selectedIcon.SetValue(item, isExpanded ? _openFolderSelectedTexture : _closedFolderSelectedTexture);
211204
}
212205
}
213206
}

0 commit comments

Comments
 (0)