Skip to content

Commit 76b68e7

Browse files
committed
Clean up minor code issues
1 parent a59c46b commit 76b68e7

File tree

2 files changed

+72
-99
lines changed

2 files changed

+72
-99
lines changed

Editor/HierarchyFolderIcon.cs

Lines changed: 53 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -2,76 +2,68 @@
22
using System.Collections;
33
using System.Collections.Generic;
44
using System.Linq;
5-
using System.Linq.Expressions;
65
using System.Reflection;
7-
using System.Runtime.Remoting.Messaging;
8-
using System.Xml;
96
using UnityEditor;
107
using UnityEditor.IMGUI.Controls;
11-
using UnityEditor.SceneManagement;
12-
using UnityEditorInternal;
138
using UnityEngine;
149
using UnityEngine.Experimental.Rendering;
1510
using UnityHierarchyFolders.Runtime;
1611
using Object = UnityEngine.Object;
1712

18-
namespace Plugins.UnityHierarchyFolders
19-
{
13+
namespace Plugins.UnityHierarchyFolders {
2014
public class HierarchyFolderIcon
2115
{
22-
private static Texture2D _openFolderTexture;
23-
private static Texture2D _closedFolderTexture;
24-
private static Texture2D _openFolderSelectedTexture;
25-
private static Texture2D _closedFolderSelectedTexture;
16+
private static Texture2D openFolderTexture;
17+
private static Texture2D closedFolderTexture;
18+
private static Texture2D openFolderSelectedTexture;
19+
private static Texture2D closedFolderSelectedTexture;
2620

27-
private static bool _isInitialized;
28-
private static bool _hasProcessedFrame;
21+
private static bool isInitialized;
22+
private static bool hasProcessedFrame;
2923

3024
// Reflected members
31-
private static PropertyInfo _sceneHierarchyProperty;
32-
private static PropertyInfo _treeViewProperty;
33-
private static PropertyInfo _dataProperty;
34-
private static MethodInfo _getRowsMethod;
35-
private static MethodInfo _isExpandedMethod;
36-
private static PropertyInfo _selectedIconProperty;
37-
private static PropertyInfo _objectPPTRProperty;
38-
private static MethodInfo _getAllSceneHierarchyWindowsMethod;
25+
private static PropertyInfo prop_sceneHierarchy;
26+
private static PropertyInfo prop_treeView;
27+
private static PropertyInfo prop_data;
28+
private static PropertyInfo prop_selectedIcon;
29+
private static PropertyInfo prop_objectPPTR;
3930

31+
private static MethodInfo meth_getRows;
32+
private static MethodInfo meth_isExpanded;
33+
private static MethodInfo meth_getAllSceneHierarchyWindows;
4034

4135
[InitializeOnLoadMethod]
4236
static void Startup()
4337
{
4438
Initialize();
45-
EditorApplication.update += RefreshFolderIcons;
39+
EditorApplication.update += ResetFolderIcons;
4640
EditorApplication.hierarchyWindowItemOnGUI += RefreshFolderIcons;
4741
}
4842

4943
private static Texture2D GetTintedTexture(Texture2D original)
5044
{
5145
var tinted = new Texture2D(original.width, original.height,
52-
original.graphicsFormat, original.mipmapCount, TextureCreationFlags.MipChain);
46+
original.graphicsFormat, original.mipmapCount, TextureCreationFlags.MipChain);
5347

5448
Graphics.CopyTexture(original, tinted);
5549
var data = tinted.GetRawTextureData<Color32>();
5650
for (int index = 0, len = data.Length; index < len; index++)
5751
{
5852
Color32 c = data[index];
5953
byte a = c.a;
60-
61-
Color.RGBToHSV(c, out float h, out float s, out float v);
6254
c = Color.HSVToRGB(0, 0, 1);
6355
c.a = a;
6456

6557
data[index] = c;
6658
}
6759

68-
var aaa = tinted.width * tinted.height * 4;
60+
var mipmapSize = tinted.width * tinted.height * 4;
6961
var offset = 0;
7062
for (int index = 0; index < tinted.mipmapCount; index++)
7163
{
7264
tinted.SetPixelData(data, index, offset);
73-
aaa >>= 2;
74-
offset += aaa;
65+
mipmapSize >>= 2;
66+
offset += mipmapSize;
7567
}
7668
tinted.Apply();
7769

@@ -80,80 +72,68 @@ private static Texture2D GetTintedTexture(Texture2D original)
8072

8173
private static void Initialize()
8274
{
83-
if (_isInitialized)
84-
{
85-
return;
86-
}
75+
if (isInitialized) { return; }
8776

88-
_closedFolderTexture = (Texture2D) EditorGUIUtility.IconContent("Folder Icon").image;
89-
_openFolderTexture = (Texture2D) EditorGUIUtility.IconContent("FolderEmpty Icon").image;
77+
closedFolderTexture = (Texture2D) EditorGUIUtility.IconContent("Folder Icon").image;
78+
openFolderTexture = (Texture2D) EditorGUIUtility.IconContent("FolderEmpty Icon").image;
9079

91-
_openFolderSelectedTexture = GetTintedTexture(_openFolderTexture);
92-
_closedFolderSelectedTexture = GetTintedTexture(_closedFolderTexture);
80+
openFolderSelectedTexture = GetTintedTexture(openFolderTexture);
81+
closedFolderSelectedTexture = GetTintedTexture(closedFolderTexture);
9382

9483
const BindingFlags BindingAll = BindingFlags.Public
9584
| BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance;
9685

97-
//public static List<SceneHierarchyWindow> GetAllSceneHierarchyWindows()
9886
var assembly = typeof(SceneView).Assembly;
9987

100-
var sceneHierarchyWindowType = assembly.GetType("UnityEditor.SceneHierarchyWindow");
101-
_getAllSceneHierarchyWindowsMethod = sceneHierarchyWindowType.GetMethod("GetAllSceneHierarchyWindows", BindingAll);
102-
_sceneHierarchyProperty = sceneHierarchyWindowType.GetProperty("sceneHierarchy");
88+
var type_sceneHierarchyWindow = assembly.GetType("UnityEditor.SceneHierarchyWindow");
89+
meth_getAllSceneHierarchyWindows = type_sceneHierarchyWindow.GetMethod("GetAllSceneHierarchyWindows", BindingAll);
90+
prop_sceneHierarchy = type_sceneHierarchyWindow.GetProperty("sceneHierarchy");
10391

104-
var sceneHierarchyType = assembly.GetType("UnityEditor.SceneHierarchy");
105-
_treeViewProperty = sceneHierarchyType.GetProperty("treeView", BindingAll);
92+
var type_sceneHierarchy = assembly.GetType("UnityEditor.SceneHierarchy");
93+
prop_treeView = type_sceneHierarchy.GetProperty("treeView", BindingAll);
10694

107-
var treeViewControllerType = assembly.GetType("UnityEditor.IMGUI.Controls.TreeViewController");
108-
_dataProperty = treeViewControllerType.GetProperty("data", BindingAll);
95+
var type_treeViewController = assembly.GetType("UnityEditor.IMGUI.Controls.TreeViewController");
96+
prop_data = type_treeViewController.GetProperty("data", BindingAll);
10997

110-
var iTreeViewDataSourceType = assembly.GetType("UnityEditor.IMGUI.Controls.ITreeViewDataSource");
111-
_getRowsMethod = iTreeViewDataSourceType.GetMethod("GetRows");
112-
_isExpandedMethod = iTreeViewDataSourceType.GetMethod("IsExpanded", new Type[] {typeof(TreeViewItem)});
98+
var type_iTreeViewDataSource = assembly.GetType("UnityEditor.IMGUI.Controls.ITreeViewDataSource");
99+
meth_getRows = type_iTreeViewDataSource.GetMethod("GetRows");
100+
meth_isExpanded = type_iTreeViewDataSource.GetMethod("IsExpanded", new Type[] {typeof(TreeViewItem)});
113101

114-
var gameObjectTreeViewItemType = assembly.GetType("UnityEditor.GameObjectTreeViewItem");
115-
_selectedIconProperty = gameObjectTreeViewItemType.GetProperty("selectedIcon", BindingAll);
116-
_objectPPTRProperty = gameObjectTreeViewItemType.GetProperty("objectPPTR", BindingAll);
102+
var type_gameObjectTreeViewItem = assembly.GetType("UnityEditor.GameObjectTreeViewItem");
103+
prop_selectedIcon = type_gameObjectTreeViewItem.GetProperty("selectedIcon", BindingAll);
104+
prop_objectPPTR = type_gameObjectTreeViewItem.GetProperty("objectPPTR", BindingAll);
117105

118-
_isInitialized = true;
106+
isInitialized = true;
119107
}
120108

121-
private static void RefreshFolderIcons()
109+
private static void ResetFolderIcons()
122110
{
123111
Initialize();
124-
_hasProcessedFrame = false;
112+
hasProcessedFrame = false;
125113
}
126114

127-
128115
private static void RefreshFolderIcons(int instanceid, Rect selectionrect)
129116
{
130-
if (_hasProcessedFrame)
131-
{
132-
return;
133-
}
134-
135-
_hasProcessedFrame = true;
117+
if (hasProcessedFrame) { return; }
118+
hasProcessedFrame = true;
136119

137-
var windows = ((IEnumerable)_getAllSceneHierarchyWindowsMethod.Invoke(null, new object[0])).Cast<EditorWindow>().ToList();
120+
var windows = ((IEnumerable)meth_getAllSceneHierarchyWindows.Invoke(null, Array.Empty<object>())).Cast<EditorWindow>().ToList();
138121
foreach (EditorWindow h in windows)
139122
{
140-
var sceneHierarchy = _sceneHierarchyProperty.GetValue(h);
141-
var treeView = _treeViewProperty.GetValue(sceneHierarchy);
142-
var data = _dataProperty.GetValue(treeView);
123+
var sceneHierarchy = prop_sceneHierarchy.GetValue(h);
124+
var treeView = prop_treeView.GetValue(sceneHierarchy);
125+
var data = prop_data.GetValue(treeView);
143126

144-
IList<TreeViewItem> rows = (IList<TreeViewItem>) _getRowsMethod.Invoke(data, new object[0]);
127+
var rows = (IList<TreeViewItem>) meth_getRows.Invoke(data, Array.Empty<object>());
145128
foreach (TreeViewItem item in rows)
146129
{
147-
Object itemObject = (Object) _objectPPTRProperty.GetValue(item);
148-
if (!itemObject || !Folder.folders.Contains(itemObject.GetInstanceID()))
149-
{
150-
continue;
151-
}
130+
var itemObject = (Object) prop_objectPPTR.GetValue(item);
131+
if (!itemObject || !Folder.IsFolder(itemObject)) { continue; }
152132

153-
var isExpanded = (bool) _isExpandedMethod.Invoke(data, new object[] {item});
133+
var isExpanded = (bool) meth_isExpanded.Invoke(data, new object[] { item });
154134

155-
item.icon = isExpanded ? _openFolderTexture : _closedFolderTexture;
156-
_selectedIconProperty.SetValue(item, isExpanded ? _openFolderSelectedTexture : _closedFolderSelectedTexture);
135+
item.icon = isExpanded ? openFolderTexture : closedFolderTexture;
136+
prop_selectedIcon.SetValue(item, isExpanded ? openFolderSelectedTexture : closedFolderSelectedTexture);
157137
}
158138
}
159139
}

Runtime/Folder.cs

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
#if UNITY_EDITOR
22
using System;
33
using System.Collections.Generic;
4-
using System.Collections.ObjectModel;
54
using System.Linq;
65
using UnityEditor;
76
#endif
87
using UnityEngine;
98

10-
namespace UnityHierarchyFolders.Runtime
11-
{
9+
namespace UnityHierarchyFolders.Runtime {
1210
#if UNITY_EDITOR
1311
/// <summary>
1412
/// <para>Extension to Components to check if there are no dependencies to itself.</para>
@@ -58,27 +56,26 @@ private Folder()
5856
private static Tool lastTool;
5957
private static Folder toolLock;
6058

61-
public static HashSet<int> folders = new HashSet<int>();
62-
//public static ReadOnlyCollection<int> folders => new ReadOnlyCollection<int>(_folders.ToList());
59+
/// <summary>
60+
/// The set of folder objects.
61+
/// </summary>
62+
private static HashSet<int> folders = new HashSet<int>();
6363

64-
private void Start()
65-
{
66-
folders.Add(gameObject.GetInstanceID());
67-
}
64+
/// <summary>
65+
/// Test if a Unity object is a folder by way of containing a Folder component.
66+
/// </summary>
67+
/// <param name="obj">Test object.</param>
68+
/// <returns>Is this object a folder?</returns>
69+
public static bool IsFolder(UnityEngine.Object obj) => folders.Contains(obj.GetInstanceID());
6870

69-
private void OnDestroy()
70-
{
71-
folders.Remove(gameObject.GetInstanceID());
72-
}
71+
private void Start() => folders.Add(gameObject.GetInstanceID());
72+
private void OnDestroy() => folders.Remove(gameObject.GetInstanceID());
7373

7474
/// <summary>Hides all gizmos if selected to avoid accidental editing of the transform.</summary>
7575
private void HandleSelection()
7676
{
7777
// ignore if another folder object is already hiding gizmo
78-
if (toolLock != null && toolLock != this)
79-
{
80-
return;
81-
}
78+
if (toolLock != null && toolLock != this) { return; }
8279

8380
if (this != null && Selection.Contains(this.gameObject))
8481
{
@@ -130,10 +127,7 @@ private void EnsureExclusiveComponent()
130127
.Where(c => c != this && !typeof(Transform).IsAssignableFrom(c.GetType()));
131128

132129
// no items means no actions anyways
133-
if (!existingComponents.Any())
134-
{
135-
return;
136-
}
130+
if (!existingComponents.Any()) { return; }
137131

138132
if (this.AskDelete())
139133
{
@@ -145,11 +139,10 @@ private void EnsureExclusiveComponent()
145139
}
146140
}
147141

148-
private void OnEnable()
149-
{
150-
// Hide inspector to prevent accidental editing of transform.
151-
this.transform.hideFlags = HideFlags.HideInInspector;
152-
}
142+
/// <summary>
143+
/// Hide inspector to prevent accidental editing of transform.
144+
/// </summary>
145+
private void OnEnable() => this.transform.hideFlags = HideFlags.HideInInspector;
153146
#endif
154147

155148
/// <summary>

0 commit comments

Comments
 (0)