代码获取Unity编辑器中的窗口

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/NippyLi/article/details/84405430

获取Unity编辑器中的窗口

  • 这段时间在做编辑器相关内容,有时候需要对Unity自带的窗口做一些操作,这里就需要用代码反射EditorWindow程序集下的类型获取到窗口

  • 代码如下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;
using System.Linq;

public class ShowAllEditorWindow : EditorWindow
{
    static List<Type> windowTypes = new List<Type>();
    static List<Type> timelineTypes = new List<Type>();
    [MenuItem("Tools/ShowAllWindow")]
    static void ShowWindow()
    {
        ShowAllEditorWindow window = EditorWindow.GetWindow<ShowAllEditorWindow>("AllEditorWindow");
        Type[] types = typeof(EditorWindow).Assembly.GetTypes();
        windowTypes = GetEditorWindow(types);
        Debug.Log(typeof(EditorWindow).Assembly.FullName);

        //这里是为了找TimeLine窗口
        Type[] timeTypes = typeof(TimelineEditor).Assembly.GetTypes();
        timelineTypes = GetEditorWindow(timeTypes);
        Debug.Log(typeof(TimelineEditor).Assembly.FullName);
        
    }


    /// <summary>
    /// 这里是循环遍历当前类型的基类,判定是否为EditorWindow,最多找1000次
    /// </summary>
    /// <param name="types"></param>
    /// <returns></returns>
    private static List<Type> GetEditorWindow(Type[] types)
    {
        List<Type> editorType = new List<Type>();
        foreach (var item in types)
        {
            int i = 1;
            Type temp = item;
            while(temp != null && i < 1000)
            {
                if(temp.BaseType == typeof(EditorWindow))
                {
                    editorType.Add(item);
                    break;
                }
                temp = temp.BaseType;
                i++;
            }
        }

        editorType.Sort((a, b) =>
        {
           return string.Compare(a.Name, b.Name);
        });
        return editorType;
    }


    Vector2 scroll = Vector2.zero;
    private void OnGUI()
    {
        EditorGUILayout.BeginVertical();
        if (GUILayout.Button("打开所有窗口"))
        {
            foreach (var item in windowTypes)
            {
                try
                {
                    EditorWindow window = EditorWindow.GetWindow(item);
                    window.Show();
                }
                catch
                {

                }
            }
        }

        if (GUILayout.Button("关闭所有窗口"))
        {
            foreach (var item in windowTypes)
            {
                EditorWindow window = EditorWindow.GetWindow(item);
                window.Close();
            }
        }

        if (GUILayout.Button("TimeLine窗口"))
        {
            foreach (var item in timelineTypes)
            {
                try
                {
                    EditorWindow ww = EditorWindow.GetWindow(item);
                    if (ww != null)
                    {
                        ww.Show();
                        Debug.Log(item.FullName);
                    }
                }
                catch
                {

                }
               
            }
        }
        scroll =  EditorGUILayout.BeginScrollView(scroll);
        foreach (var item in windowTypes)
        {
            if (GUILayout.Button(item.Name))
            {
                if (item.Name == "SceneView")
                    Debug.Log(item.FullName);
                EditorWindow window =  EditorWindow.GetWindow(item);
                window.Show();
            }
        }
        EditorGUILayout.EndScrollView();

        EditorGUILayout.BeginVertical();
    }

}

窗口名字dll所在路径

注意:

因为有些功能是新增的,不在UnityEditor所在dll下,要么找到对应的类找到其所在程序集(有时候要靠运气),要么直接去Unity按装目录找一下有哪些dll,手动加载路径反射出来,如上图

猜你喜欢

转载自blog.csdn.net/NippyLi/article/details/84405430