Editor 代码整理Hierarchy面板,实用技能Get

这是按字母升序排列
[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);
    }
}


别担心,的类名是可以使用中文名的,你就大胆的使用吧。

如果你不满足于只是下拉选择框是中文的,还希望上面的图标也变成中文,没关系,一样可以搞定,只需复写一下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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值