因为 项目文件结构日渐复杂,unity的Project窗口操作又不是很人性化,所以我需要一个快速进入Project窗口中 文件夹的功能

public class EditorProjectToolWindow:BaseOdinEditorWindow
{
    [Serializable]
    public class Collect
    {
        public string name;
        public string path;
    }

    public static List<Collect> collects=new List<Collect>()
    {
        new Collect(){name="预制体",path="Game/Prefabs"},
        new Collect(){name="配置表的预制体",path="Game/Resources/Prefabs"},
        new Collect(){name="脚本",path="Game/Scripts"},
        new Collect(){name="美术资源",path="Game/Sources"},
        new Collect(){name="场景",path="Game/Scenes"},
        new Collect(){name="年代关卡",path="Game/Level"},
    };

    [MenuItem("EditorTool/EditorProjectWindow")]
    public static void Open()
    {
        var window = GetWindow<EditorProjectToolWindow>();
        window.titleContent = new GUIContent("收藏夹工具");
        window.Show();
    }
    protected override void OnGUI()
    {
        int h = 0;

        for (int i = 0; i < collects.Count; i++)
        {
            if (h==0)
            {
                EditorGUILayout.BeginHorizontal();
            }
            if (GUILayout.Button(collects[i].name))
            {
                var obj = AssetDatabase.LoadAssetAtPath("Assets/"+ collects[i].path,typeof(Object));
                EditorGUIUtility.PingObject(obj);
                AssetDatabase.OpenAsset(obj);
            }

            h++;
            if (h>=3)
            {
                h = 0;
                EditorGUILayout.EndHorizontal();
            }
        }
    }
}

点击按钮进入文件夹 不用再在Project窗口里一直找了

主要就是通过EditorGUIUtility.PingObject(obj)选中对象,然后通过AssetDatabase.OpenAsset(obj)打开它

不过现在这样要点两次才能进入文件夹。。而且每次运行后,重新编译后都要点一下Project窗口,才能点两次进入文件夹,不然还是停在在父文件夹中选中该文件夹的状态,本来打算 用 user32.dll 控制鼠标 点击一下Project窗让它获取焦点的,但试了一下效果不好,于是作罢保持现状了

Last modification:August 19th, 2021 at 10:35 am