Buy Me a Coffee at ko-fi.com

思路

  • 新建一个Tag Folder 用于标识显示为Folder的Empty GameObject
  • 针对Tag为Folder的GameObject进行特殊绘制处理

用到的UnityEditor方法

Full Code

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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
using UnityEngine;
using UnityEditor;

namespace kkmaple.EditorTool
{
[InitializeOnLoad]
public partial class CustomHierarchyFolder
{
#region 静态构造
static CustomHierarchyFolder()
{
EditorApplication.hierarchyWindowItemOnGUI += HierarchyWindowItemOnGUI;
}
#endregion
#region 变量
public static GameObject selectObj;
#endregion
#region 菜单
[MenuItem("GameObject/Folder/Create Folder", false, -1)]
static void CreateEmptyFolder()
{
GameObject obj = new GameObject();
Undo.RegisterCreatedObjectUndo(obj, obj.name);
if (Selection.activeTransform != null)
{
Object[] selectedObjects = Selection.objects;
GameObject selectedGameObject = selectedObjects[0] as GameObject;
obj.transform.SetParent(selectedGameObject.transform);
}
obj.name = "New Folder";
EditorUtility.SetDirty(obj);
Selection.activeGameObject = obj;
AddTag(obj, "Folder");

}
[MenuItem("GameObject/Folder/Create Folder", true, -1)]
private static bool ValidateCreateEmpty()
{
return Selection.objects.Length <= 1;
}
#endregion
#region 工具方法
/// <summary>
/// 向工程中添加tag
/// </summary>
/// <param name="tag">tag名</param>
public static void AddTagToProject(string tag)
{
Object[] asset = AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset");
if ((asset != null) && (asset.Length > 0))
{
SerializedObject so = new SerializedObject(asset[0]);
SerializedProperty tags = so.FindProperty("tags");
for (int i = 0; i < tags.arraySize; ++i)
{
if (tags.GetArrayElementAtIndex(i).stringValue == tag)
{
//tag已经存在啥也不做
return;
}
}
tags.InsertArrayElementAtIndex(0);
tags.GetArrayElementAtIndex(0).stringValue = tag;
so.ApplyModifiedProperties();
so.Update();
}
}
/// <summary>
/// 给指定对象添加tag
/// </summary>
/// <param name="obj"></param>
/// <param name="tag"></param>
public static void AddTag(GameObject obj, string tag)
{
AddTagToProject(tag);
obj.tag = tag;
}
/// <summary>
/// 重置GameObject位置
/// </summary>
/// <param name="obj"></param>
internal static void TransfromReset(GameObject obj)
{
obj.transform.localPosition = Vector3.zero;
obj.transform.localRotation = Quaternion.identity;
obj.transform.localScale = Vector3.one;
}
/// <summary>
/// 勾选按钮
/// </summary>
/// <param name="obj"></param>
/// <param name="selectionRect"></param>
internal static void EnableDisableToggle(GameObject obj, Rect selectionRect)
{
Rect r = selectionRect;
r.xMax = r.width;
r.x = selectionRect.xMax;
var tmpColor = GUI.color;
//设置透明
GUI.color = new Color(0, 0, 0, 0f);
if (GUI.Button(r, ""))
{
obj.SetActive(!obj.activeInHierarchy);
}
//还原颜色
GUI.color = tmpColor;
r.x = selectionRect.xMax;
GUI.Label(r, obj.activeInHierarchy ? EditorGUIUtility.IconContent("d_toggle_on_focus") : EditorGUIUtility.IconContent("d_toggle_bg"));
}
/// <summary>
/// 检查工程中是否存在tag
/// </summary>
/// <param name="tag">tag名</param>
public static bool IsTagExsist(string tag)
{
Object[] asset = AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset");
if ((asset != null) && (asset.Length > 0))
{
SerializedObject so = new SerializedObject(asset[0]);
SerializedProperty tags = so.FindProperty("tags");
bool exsist = false;
for (int i = 0; i < tags.arraySize; ++i)
{
if (tags.GetArrayElementAtIndex(i).stringValue == tag)
{
exsist = true;
break;
}
else
{
exsist = false;
}
}
if (exsist) return true;
else { return false; }
}
else
{
return false;
}
}
/// <summary>
/// 处理Folder显示
/// </summary>
/// <param name="instanceID">Object id</param>
/// <param name="selectionRect"></param>
internal static void HierarchyWindowItemOnGUI(int instanceID, Rect selectionRect)
{
GameObject obj = EditorUtility.InstanceIDToObject(instanceID) as GameObject;

if (obj == null)
{
selectObj = null;
return;
}
if (obj.transform == Selection.activeTransform)
{
selectObj = obj;
}

Rect r = selectionRect;

if (!IsTagExsist("Folder")) return;

//处理Folder显示
if (obj.CompareTag("Folder"))
{
EnableDisableToggle(obj, selectionRect);

//重置位置
TransfromReset(obj);

//隐藏编辑选项
obj.transform.hideFlags = HideFlags.NotEditable | HideFlags.HideInInspector;
obj.hideFlags = HideFlags.HideInInspector;

//在Hierarchy中不可pick
var sv = SceneVisibilityManager.instance;
sv.DisablePicking(obj, false);

GUI.color = new Color(1, 1, 1, 1);
r.xMin = selectionRect.x + 1;
r.xMax = r.xMin + 15f;

bool isPro = EditorGUIUtility.isProSkin;
if (obj.transform == Selection.activeTransform)
{
if (isPro) EditorGUI.DrawRect(r, new Color(0.17f, 0.36f, 0.52f, 1f));
else { EditorGUI.DrawRect(r, new Color(0.23f, 0.45f, 0.69f, 1f)); }
}
else
{
if (isPro) EditorGUI.DrawRect(r, new Color(0.22f, 0.22f, 0.22f, 1f));
else { EditorGUI.DrawRect(r, new Color(0.8f, 0.8f, 0.8f, 1f)); }
}

r = selectionRect;
r.x = selectionRect.xMin + 25;

if (obj.activeInHierarchy)
{
ChangeFolderIconActive(obj, selectionRect);
}
else
{
ChangeFolderIconDactive(obj, selectionRect);
}
}
}
/// <summary>
/// Folder Icon Active
/// </summary>
static void ChangeFolderIconActive(GameObject obj, Rect selectionRect)
{
int childcout = obj.transform.childCount;
bool isExtended = true;
string iconeName = string.Empty;
bool hasChild = childcout > 0;
if (hasChild)
{
if (EditorGUIUtility.isProSkin)
{
if (isExtended)
{
iconeName = "FolderOpened On Icon";
}
else
{
iconeName = "Folder On Icon";
}
}
else
{
if (isExtended)
{
iconeName = "FolderOpened Icon";
}
else
{
iconeName = "Folder Icon";
}
}
}
else
{
if (EditorGUIUtility.isProSkin)
{
iconeName = "FolderEmpty On Icon";
}
else
{
iconeName = "FolderEmpty Icon";
}
}
EditorGUI.LabelField(selectionRect, EditorGUIUtility.IconContent(iconeName));
}

/// <summary>
/// Folder Icon Dactive
/// </summary>
static void ChangeFolderIconDactive(GameObject obj, Rect selectionRect)
{
int childcout = obj.transform.childCount;
bool isExtended = true;
string iconeName = string.Empty;
bool hasChild = childcout > 0;
if (hasChild)
{
if (EditorGUIUtility.isProSkin)
{
if (isExtended)
{
iconeName = "FolderOpened Icon";
}
else
{
iconeName = "Folder Icon";
}
}
else
{
if (isExtended)
{
iconeName = "FolderOpened On Icon";
}
else
{
iconeName = "Folder On Icon";
}
}
}
else
{
if (EditorGUIUtility.isProSkin)
{
iconeName = "FolderEmpty Icon";
}
else
{
iconeName = "FolderEmpty On Icon";
}
}
EditorGUI.LabelField(selectionRect, EditorGUIUtility.IconContent(iconeName));
}
}
#endregion
}