How do I get a reference to the default editor windows (Hierarchy, Console, and Inspector)?

I want to be able to get references to the default editor windows (inspector, hierarchy, console etc…) So that i can close, open and focus my own windows using them.

e.g. I want to do something like:

HierarchyWindow myWindow = (HierarchyWindow) EditorWindow.GetWindow();

thanks!

Most built-in editor windows are internal classes, so you don’t have direct access to those classes. You can use reflection if you want to access certain properties / fieldss / methods of those windows.

The only way to access them in the first place is by using reflection to get a System.Type reference of the desired class. This can be aquired by using the GetTypes method of the Assembly class of the UnityEditor assembly.

public static System.Type[] GetAllEditorWindowTypes()
{
    var result = new System.Collections.Generic.List<System.Type>();
    System.Reflection.Assembly[] AS = System.AppDomain.CurrentDomain.GetAssemblies();
    System.Type editorWindow = typeof(EditorWindow);
    foreach (var A in AS)
    {
        System.Type[] types = A.GetTypes();
        foreach (var T in types)
        {
            if (T.IsSubclassOf(editorWindow))
                result.Add(T);
        }
    }
    return result.ToArray();
}

This method will return all editor window types it can find in any loaded assembly (so even your own).
Those types can be used with GetWindow or FindObjectsOfTypeAll to open / find a certain editorwindow

Though if you just want to find all open EditorWindows you can directly use Resources.FindObjectsOfTypeAll:

EditorWindow[] allWindows = Resources.FindObjectsOfTypeAll<EditorWindow>();

Now you just have to filter out the ones you need based either on the type name ( win.GetType().Name ) or on the window title ( win.titleContent.text ).

However!

I don't think this would make any sense.

First of all there is nothing like a “default editor window”. The user can decide which windows he wants to use and how many. You can open 3 scene views, 2 inspectors and no console at all. So trying to “access” the console window when it’s not open will fail. Also having multiple windows of a certain type would need special handling.

Next thing is you can’t “close” a window without destroying the window instance. So all current settings of that window will be reset when you re-open it. Closing a window has the same effect as clicking the little “x” at the top right of the window / tab. You can’t “hide” a window.

Next thing is when you close a window that is currently docked you will rip the layout apart. Currently Unity doesn’t provide a nice way to build up the layout from code. All required classes for this are internal (View, GUIView, HostView, DockArea, SplitView, MainWindow, ContainerWindow, …).

And finally the most important point: Most users wouldn’t use any tool that decides how their layout should look like or that closes other windows when it thinks it’s required. Personally i work with a two monitor setup and i often have the gameview and some other windows on the second monitor. Other people may have more than two monitors and spread their windows across them. Closing / opening / moving / docking arbitrary windows automatically doesn’t make any sense for any kind of tool unless it’s a “window management tool”

You can do it like this:

var type = typeof(EditorWindow).Assembly.GetType("UnityEditor.SceneHierarchyWindow");
var window = EditorWindow.GetWindow(type);