Unit editor - get all the windows in the unit editor and open the display for later editor expansion

Keywords: Windows Unity

Getting principle: all window interfaces of unity are inherited from the EditorWindow under the UnityEditor assembly. All editor windows are defined in the UnityEditor assembly, so we can get all the windows by getting the UnityEditor assembly through reflection.

Direct code:

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>
    ///Get all window types
    /// </summary>
    /// <returns></returns>
    static List<Type> getWindowAll()
    {
        Assembly assembly = typeof(EditorWindow).Assembly; //Get the UnityEditor assembly. Of course, you can also load the UnityEditor assembly directly to get it. The figure here is convenient. For the specific method, see the assembly loading 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); });  //sort
        return list;
    }
    /// <summary>
    ///Determine whether it is an editor window
    /// </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>
    ///Close all windows
    /// </summary>
    void closeWindowAll()
    {
        for (int i = 0; i < windowList.Count; i++)
        {
            try
            {
                EditorWindow editorWindow = EditorWindow.GetWindow(windowList[i]);
                if (editorWindow)
                {
                    editorWindow.Close();           //close window
                }
            }
            catch
            {

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

            }
        }
    }
    /// <summary>
    ///Display the specified type window
    /// </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("Close all windows"))
        {
            closeWindowAll();
        }
        //if (GUILayout.Button("open all windows")
        //{
        //    showWindowAll();
        //}
        pos = GUILayout.BeginScrollView(pos);
        for (int i = 0; i < windowList.Count; i++)
        {
            if (GUILayout.Button(windowList[i].Name))
            {
                showWindow(windowList[i]);
            }
        }
        GUILayout.EndScrollView();
    }
}

The code is annotated and will not be interpreted any more. editorWindow.Close() closes the window. editorWindow.Show() show window

First, close all windows as shown in the figure below: the interface is really clean

Then we can open the window of unity by clicking the button above:

 

Posted by bkanmani on Mon, 16 Dec 2019 13:48:24 -0800