Unity编辑器拓展之十七:NGUI ComponentSelector增加搜索框

NGUI ComponentSelector增加搜索框

目的

最近在使用NGUI,给Sprite选择图集的时候,没有搜索框要挨个找挺麻烦,因此修改一下ComponentSelector组件,增加一个搜索框,便于搜索图集

这里写图片描述

Gif动图展示

这里写图片描述

SearchField介绍

关于SearchField介绍请看 Unity编辑器拓展之九

代码

以下代码是修改的NGUI里的ComponentSelector脚本

private SearchField search;
private bool isInit = false;
private string searchStr = "";
private void IfNeedInit()
{
    if(isInit == false)
        search = new SearchField();
    isInit = true;
}

void OnGUI ()
{
    IfNeedInit();
    searchStr = search.OnGUI(new Rect(10,10,200,10),searchStr);
    searchStr = searchStr.ToLower();
    //省略后面源码    
}
bool DrawObject (Object obj)
{
    if (obj == null) return false;

    //搜索
    if(!string.IsNullOrEmpty(searchStr) && !obj.name.ToLower().Contains(searchStr)) return false;

    bool retVal = false;
    Component comp = obj as Component;
    GUILayout.BeginHorizontal();
    {
        string path = AssetDatabase.GetAssetPath(obj);
        if (string.IsNullOrEmpty(path))
        {
            path = "[Embedded]";
            GUI.contentColor = new Color(0.7f, 0.7f, 0.7f);
        }
        else if (comp != null && EditorUtility.IsPersistent(comp.gameObject))
            GUI.contentColor = new Color(0.6f, 0.8f, 1f);

        retVal |= GUILayout.Button(obj.name, "AS TextArea", GUILayout.Width(160f), GUILayout.Height(20f));
        retVal |= GUILayout.Button(path.Replace("Assets/", ""), "AS TextArea", GUILayout.Height(20f));
        GUI.contentColor = Color.white;

        retVal |= GUILayout.Button("Select", "ButtonLeft", GUILayout.Width(60f), GUILayout.Height(16f));
    }
    GUILayout.EndHorizontal();
    return retVal;
}

以上知识分享,如有错误,欢迎指出,共同学习,共同进步。

猜你喜欢

转载自blog.csdn.net/qq_26999509/article/details/81322620