在日常的工作和研究中,當給我們的場景擺放過多的物件的時候,Hierarchy面板就會變得雜亂不堪。比如這樣:
過多的層次結構充斥在里面,根層的物件毫無序列可言,整個層次面板顯示非常的雜亂不堪,如果還有使用代碼添加的物件,那就更加的慘不忍睹。里面的物件沒有任何的規律可言(當然如果你們的美術有強迫症的話,也許會把物件分類,按規律排列的整齊,如果不是就慘了)。如果費時費力的排列好里面的結構,過一段時間就又會變亂。
而如果要在雜亂的層次結構中找到我們想要的物體就需要費些體力和眼神了,就如同在垃圾堆里找寶石一樣。
如果Hierarchy能按字母排序的話,那該多好!一個簡單的字母排序,就會讓整個結構看起來都是規規矩矩、整整齊齊。不論怎樣也都會好過沒有排序的。
比如下面這樣:
別放棄,天無絕人之路,想讓Hierarchy按字母排序,非常的簡單,整個文件只有10行代碼,其中using xxx占用了2行,符號占用2行,類名和函數名各1行,真正工作的代碼只有4行。
代碼結構就是下面這樣
效果就是下面這個樣子

哈哈,不逗你了,下面開始說正經事了!!!
這是按字母升序排列
|
1
2
3
4
5
6
7
8
|
public class AscendingSort : BaseHierarchySort {
public override int Compare( GameObject lhs , GameObject rhs) {
if (lhs == rhs) { return 0; }
if (lhs == null) { return -1; }
if (rhs == null) { return 1; }
return EditorUtility .NaturalCompare( lhs.name , rhs.name);
}
}
|
按字母降序排列
|
1
2
3
4
5
6
7
8
|
public class DescendingSort : BaseHierarchySort {
public override int Compare( GameObject lhs , GameObject rhs) {
if (lhs == rhs) { return 0; }
if (lhs == null) { return 1; }
if (rhs == null) { return -1; }
return EditorUtility .NaturalCompare( rhs.name , lhs.name);
}
}
|
按InstanceID排序
|
1
2
3
4
5
6
7
8
|
public class InstanceIDSort : BaseHierarchySort {
public override int Compare( GameObject lhs , GameObject rhs) {
if (lhs == rhs) { return 0; }
if (lhs == null) { return -1; }
if (rhs == null) { return 1; }
return lhs .GetInstanceID(). CompareTo(rhs .GetInstanceID());
}
}
|
按HashCode排序
|
1
2
3
4
5
6
7
8
|
public class HashCodeSort : BaseHierarchySort {
public override int Compare( GameObject lhs , GameObject rhs) {
if (lhs == rhs) { return 0; }
if (lhs == null) { return -1; }
if (rhs == null) { return 1; }
return lhs .GetHashCode(). CompareTo(rhs .GetHashCode());
}
}
|
InstanceID排序與HashCode排序是一樣的,沒有看出其中的差異。
當然除了排序,我們還可以干點其他的,比如把排序下拉框改成中文的,一樣很簡單,如下
如果想要你的下拉選項變成中文的,沒關系一樣可以搞定(以升序排列為例),如下
|
1
2
3
4
5
6
7
8
|
public class 升序排列: BaseHierarchySort {
public override int Compare( GameObject lhs , GameObject rhs) {
if (lhs == rhs) { return 0; }
if (lhs == null) { return -1; }
if (rhs == null) { return 1; }
return EditorUtility .NaturalCompare( lhs.name , rhs.name);
}
}
|
別擔心,Unity的類名是可以使用中文名的,你就大膽的使用吧。
如果你不滿足於只是下拉選擇框是中文的,還希望上面的圖標也變成中文,沒關系,一樣可以搞定,只需復寫一下content就可以了
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public class 升序排列 : BaseHierarchySort
{
public override int Compare( GameObject lhs , GameObject rhs)
{
if (lhs == rhs) { return 0; }
if (lhs == null) { return -1; }
if (rhs == null) { return 1; }
return EditorUtility .NaturalCompare( lhs.name , rhs.name);
}
public override GUIContent content {
get { return new GUIContent( "升序"); }
}
}
|
顯示圖片也是沒有問題的哦,給個圖文混合顯示的吧
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
public class AscendingSort : BaseHierarchySort {
private readonly GUIContent _content;
public AscendingSort() {
Texture2D image = Resources. Load<Texture2D >("Fire");
if (image ) {
_content = new GUIContent( "升序", image , "升序排列");
}
else {
_content = new GUIContent( "升序", "升序排列" );
}
}
public override GUIContent content {
get { return _content; }
}
public override int Compare( GameObject lhs , GameObject rhs) {
if (lhs == rhs) { return 0; }
if (lhs == null) { return -1; }
if (rhs == null) { return 1; }
return EditorUtility .NaturalCompare( lhs.name , rhs.name);
}
}
|
當然上面的也可以換成自定義的圖片,自定義文字,自定義圖片+文字,也可以給與美術進行提示等等。全部只看你返回的是一個什么樣的content了,這里就不做更多的介紹了
