- Home /
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!
I Don't think there is any possibility, However some of the Windows functionality can be referenced, But I am not sure if you can code to open/close Inspector from the script. Think about it, its only silly, when you can adjust those windows easily with more functionality rather than open/close, so why code it. Its like coding the Unity. Its funny when Unity says I'll let you code, and here we are discussing let's code unity, Bruv!! Still its only my experience, what not can be possible right, Say I am optimistic! But, yeah, You might wanna ask in the moderators forum, If you're that serious.
Cheers!
Hey, I think this might help you : http://answers.unity3d.com/questions/303785/how-can-i-create-a-component.html
Answer by Bunny83 · Sep 09, 2016 at 06:08 AM
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"
This is extremely useful. Thank you for answering the question, even if you don't think it's useful.
I'm working on a simple FPS. When the player presses escape, my code does stuff like freezing the world, bringing up the GUI, etc. I have a #if UNITY_EDITOR... #endif
thing in there for extra stuff to do. For example the Quit button makes the editor game stop playing. When I press go to a menu, I want to make the game window $$anonymous$$imize, so I can do stuff like enable/disable the ceiling and check error logs, such as why is this object not referenced?
I know I'm spending a long-ass time trying to figure out how to save myself two button clicks. But it shouldn't take a long-ass time to accomplish. So thank you for giving this answer.
To clarify: EditorWindow[] allWindows = Resources.FindObjectsOfTypeAll<EditorWindow>();
and filtering is what I found useful.
Hello, if I do System.Type[] allUnityWindow = GetAllEditorWindowTypes();
with your custom function, how can I get the reference as EditorWindow ?
$$anonymous$$y Goad is to Open or focus an Animation window in the inspector. I have managed to find the animation window if it exist, and focus it. But if it doesn't exist, I would like to Create it / open it...
EditorWindow[] allWindows = Resources.FindObjectsOfTypeAll<EditorWindow>();
bool isOpen = false;
for (int i = 0; i < allWindows.Length; i++)
{
Debug.Log(allWindows[i].titleContent.text);
if (string.Equals(allWindows[i].titleContent.text, "Animation"))
{
allWindows[i].Focus();
isOpen = true;
}
}
if (!isOpen)
{
Debug.Log("create window !");
System.Type[] allUnityWindow = UtilityEditor.GetAllEditorWindowTypes();
for (int i = 0; i < allUnityWindow.Length; i++)
{
//here How can I test for Animation window, and Create one ?
}
}
Any EditorWindow can be opened with EditorWindow.GetWindow. Of course when you use a System.Type value you can't use the generic version since the generic version requires an actual type, not a System.Type instance.
Just read my answer carefully. The last sentence before the big "However" gave you the information / hint you need. As i said, the actual classnames of the built-in editor windoes are internal classes. So Unity could change them at any time since they are not documented nor part of the API they provide to us users.
Please, next time when you have a question or if something isn't clear, ask your own question ins$$anonymous$$d of hijacking someone else's question. The format of a Q&A site is simple. One question at the top, answers follows.
Your answer
Follow this Question
Related Questions
Text in custom editor is displayed/rendered with boxes around characters... 2 Answers
Editor GUI Foldout header style customization 0 Answers
Is it possible to store and display EditorGUILayout.Toggles? 0 Answers
How to get EditorWindow client rect? 1 Answer
Does Unity IMGUI for Editor use DirectX on Windows? 0 Answers