1. 程式人生 > >unity 編輯器 ---獲取unity編輯器中的所有視窗,並開啟顯示,以備後面編輯器擴充套件

unity 編輯器 ---獲取unity編輯器中的所有視窗,並開啟顯示,以備後面編輯器擴充套件

獲取原理:unity 所有視窗介面都繼承自編輯器UnityEditor程式集下的EditorWindow。而所有的編輯器視窗都在UnityEditor程式集裡定義,所以,我們通過反射獲取UnityEditor程式集獲取所有視窗就可以了。

直接上程式碼:

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

class ZzgWindow : EditorWindow
{
    static List<Type> windowList = new List<Type>();
    [MenuItem("TestContextMenu/Open Window")]

    static void Init()
    {
        var window = GetWindow(typeof(ZzgWindow));
        window.titleContent = new GUIContent("ZZG");
        window.position = new Rect(200, 200, 400, 800);
        window.Show();
        windowList = getWindowAll();
    }

    [MenuItem("TestContextMenu/getWindowAll")]

    static void getWindow()
    {
        windowList = getWindowAll();
    }
    /// <summary>
    /// 獲取所有視窗型別
    /// </summary>
    /// <returns></returns>
    static List<Type> getWindowAll()
    {
        Assembly assembly = typeof(EditorWindow).Assembly; //獲取UnityEditor程式集,當然你也可以直接載入UnityEditor程式集來獲取,我這裡圖方便,具體方法看一下程式集的載入Assembly.Load();
        Type[] types = assembly.GetTypes();
        List<Type> list =new List<Type>();
        for (int i = 0; i < types.Length; i++)
        {
            if (isEditorWindow(types[i]))
            {
                if (types[i].Name == "GameView")
                {
                    Debug.Log(types[i].FullName);
                }

                if (types[i].Name == "SceneView")
                {
                    Debug.Log(types[i].FullName);
                }
                list.Add(types[i]);
            }
            
        }
        list.Sort((a,b)=> { return string.Compare(a.Name, b.Name); });  //排序
        return list;
    }
    /// <summary>
    /// 判斷是否是編輯器視窗
    /// </summary>
    /// <param name="type"></param>
    /// <returns></returns>
    static bool isEditorWindow(Type type)
    {
        int i = 0;
        Type temp = type;
        while (null !=temp&&i<10000)
        {
            i++;
            if (temp.BaseType == typeof(EditorWindow))
            {
                return true;
            }
            temp = temp.BaseType;
        }
        return false;
    }
    /// <summary>
    /// 關閉所有視窗
    /// </summary>
    void closeWindowAll()
    {
        for (int i = 0; i < windowList.Count; i++)
        {
            try
            {
                EditorWindow editorWindow = EditorWindow.GetWindow(windowList[i]);
                if (editorWindow)
                {
                    editorWindow.Close();           //關閉視窗
                }
            }
            catch
            {

            }
        }
    }
    void showWindowAll()
    {
        for (int i = 0; i < windowList.Count; i++)
        {
            try
            {
                EditorWindow editorWindow = EditorWindow.GetWindow(windowList[i]);
                if (editorWindow)
                {
                    editorWindow.Show();        //開啟視窗
                }
            }
            catch
            {

            }
        }
    }
    /// <summary>
    /// 顯示指定型別視窗
    /// </summary>
    /// <param name="type"></param>
    void showWindow(Type type)
    {
        try
        {
            EditorWindow editorWindow = EditorWindow.GetWindow(type);
            if (editorWindow)
            {
                editorWindow.Show();
            }
        }
        catch
        {

        }
    }
    Vector2 pos = new Vector2(0,0);
    void OnGUI()
    {
        if (GUILayout.Button("關閉所有視窗"))
        {
            closeWindowAll();
        }
        //if (GUILayout.Button("開啟所有視窗"))
        //{
        //    showWindowAll();
        //}
        pos = GUILayout.BeginScrollView(pos);
        for (int i = 0; i < windowList.Count; i++)
        {
            if (GUILayout.Button(windowList[i].Name))
            {
                showWindow(windowList[i]);
            }
        }
        GUILayout.EndScrollView();
    }
}

程式碼有註釋,不再解釋了。editorWindow.Close()關閉視窗。editorWindow.Show()顯示視窗

首先關閉所有視窗如下圖:介面真乾淨

然後我們通過點選上面的按鈕開啟unity自帶的視窗: