本帖隱藏的內容
既然你已經回覆,那就告訴你把
這是按字母升序排列
[C#] 純文本查看 複製代碼
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);
}
}
按字母降序排列
[C#] 純文本查看 複製代碼
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排序
[C#] 純文本查看 複製代碼
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排序
[C#] 純文本查看 複製代碼
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排序是一樣的,沒有看出其中的差異。
當然除了排序,我們還可以乾點其他的,比如把排序下拉框改成中文的,一樣很簡單,如下
如果想要你的下拉選項變成中文的,沒關係一樣可以搞定(以升序排列爲例),如下
[C#] 純文本查看 複製代碼
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就可以了
[C#] 純文本查看 複製代碼
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( "升序"); }
}
}
顯示圖片也是沒有問題的哦,給個圖文混合顯示的吧
[C#] 純文本查看 複製代碼
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了,這裏就不做更多的介紹了
項目工程下載地址 https://github.com/sevenfires/HierarchySort.git