- Home /
Editor programming experts: regarding 'Layouts'...
Note -- click here
http://answers.unity3d.com/questions/395202/editor-scripting-unity-4-problem-with-project-wind.html
for an update re Unity4 syntax.
Dear experts at programming the editor ...
My problem, the "Layouts" system in Unity ...
... does not look after the sizes of windows. Would it be possible to program something that can set your window sizes? Can you access that info and move the windows around? Thanks!
Note Incredibly, jspease has produced an astounding full solution for this, which he offers freely, enjoy his amazing code below.
Answer by jspease · Jun 15, 2012 at 09:52 AM
This can mostly be done with editor classes, but there's a big problem due to a hole in the API: There's no way to dock a window from script code and there's no way to move a window without undocking it. But here's some code anyway:
using System;
using UnityEditor;
using UnityEngine;
public class WindowLayout
{
[MenuItem("Window/LAYOUT TEST", false, 99)]
public static void LayoutTestCommand()
{
// initialize window-specific title data and such (this can be cached)
WindowInfos windows = new WindowInfos();
// print some position
if(windows.game.isOpen)
Debug.Log("game window was open at " + windows.game.position);
else
Debug.Log("game window was closed");
// move some windows to various places, and open them if they aren't already open
windows.game.position = new Rect(100,100,300,300); // left,top,width,height
windows.inspector.position = new Rect(400,200,200,400);
windows.hierarchy.position = new Rect(600,100,100,400);
windows.project.position = new Rect(700,100,100,400);
windows.console.position = new Rect(200,550,600,150);
// ensure a window is open without moving it from its previous position
windows.scene.isOpen = true;
// close a window if it was open
windows.animation.isOpen = false;
}
class WindowInfos
{
// note: some of this data might need to change a little between different versions of Unity
public WindowInfo scene = new WindowInfo("UnityEditor.SceneView", "Scene", "Window/Scene");
public WindowInfo game = new WindowInfo("UnityEditor.GameView", "Game", "Window/Game");
public WindowInfo inspector = new WindowInfo("UnityEditor.InspectorWindow", "Inspector", "Window/Inspector");
public WindowInfo hierarchy = new WindowInfo("UnityEditor.HierarchyWindow", "Hierarchy", "Window/Hierarchy");
public WindowInfo project = new WindowInfo("UnityEditor.ProjectWindow", "Project", "Window/Project");
public WindowInfo animation = new WindowInfo("UnityEditor.AnimationWindow", "Animation", "Window/Animation");
public WindowInfo profiler = new WindowInfo("UnityEditor.ProfilerWindow", "Profiler", "Window/Profiler");
public WindowInfo console = new WindowInfo("UnityEditor.ConsoleWindow", "Console", "Window/Console");
public WindowInfo navigation = new WindowInfo("UnityEditor.NavMeshEditorWindow", "Navigation", "Window/Navigation");
public WindowInfo occlusion = new WindowInfo("UnityEditor.OcclusionCullingWindow", "Occlusion", "Window/Occlusion Culling");
public WindowInfo lightmapping = new WindowInfo("UnityEditor.LightmappingWindow", "Lightmapping", "Window/Lightmapping");
public WindowInfo assetServer = new WindowInfo("UnityEditor.ASMainWindow", "Server", "Window/Asset Server");
public WindowInfo assetStore = new WindowInfo("UnityEditor.AssetStoreWindow", "Asset Store", "Window/Asset Store");
public WindowInfo particle = new WindowInfo("UnityEditor.ParticleSystemWindow", "Particle Effect", "Window/Particle Effect");
public MainWindow main = new MainWindow();
}
class WindowInfo
{
string defaultTitle;
string menuPath;
Type type;
public WindowInfo(string typeName, string defaultTitle=null, string menuPath=null, System.Reflection.Assembly assembly=null)
{
this.defaultTitle = defaultTitle;
this.menuPath = menuPath;
if(assembly == null)
assembly = typeof(UnityEditor.EditorWindow).Assembly;
type = assembly.GetType(typeName);
if(type == null)
Debug.LogWarning("Unable to find type \"" + typeName + "\" in assembly \"" + assembly.GetName().Name + "\".\nYou might want to update the data in WindowInfos.");
}
public EditorWindow[] FindAll()
{
if(type == null)
return new EditorWindow[0];
return (EditorWindow[])(Resources.FindObjectsOfTypeAll(type));
}
public EditorWindow FindFirst()
{
foreach(EditorWindow window in FindAll())
return window;
return null;
}
public EditorWindow FindFirstOrCreate()
{
EditorWindow window = FindFirst();
if(window != null)
return window;
if(type == null)
return null;
if(menuPath != null && menuPath.Length != 0)
EditorApplication.ExecuteMenuItem(menuPath);
window = EditorWindow.GetWindow(type, false, defaultTitle);
return window;
}
// shortcut for setting/getting the position and size of the first window of this type.
// when setting the position, if the window doesn't exist it will also be created.
public Rect position
{
get
{
EditorWindow window = FindFirst();
if(window == null)
return new Rect(0,0,0,0);
return window.position;
}
set
{
EditorWindow window = FindFirstOrCreate();
if(window != null)
window.position = value;
}
}
// shortcut for deciding if any windows of this type are open,
// or for opening/closing windows
public bool isOpen
{
get
{
return FindAll().Length != 0;
}
set
{
if(value)
FindFirstOrCreate();
else
foreach(EditorWindow window in FindAll())
window.Close();
}
}
}
// experimental support for getting at the main Unity window
class MainWindow
{
Type type;
UnityEngine.Object window;
public MainWindow()
{
type = typeof(UnityEditor.EditorWindow).Assembly.GetType("UnityEditor.ContainerWindow");
if(type != null)
{
foreach(UnityEngine.Object w in Resources.FindObjectsOfTypeAll(type))
{
object parent = type.InvokeMember("get_mainView", System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Instance, null, w, null);
if(parent != null && parent.GetType().Name.Contains("MainWindow"))
{
window = w;
break;
}
}
}
if(window == null)
Debug.LogWarning("Unable to find main window.\nMaybe you'll need to update the MainWindow constructor for your version of Unity.");
}
public Rect position
{
get
{
if(window == null)
return new Rect(0,0,0,0);
return (Rect)type.InvokeMember("get_position", System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Instance, null, window, null);
}
set
{
if(window != null)
type.InvokeMember("set_position", System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Instance, null, window, new object[]{ value });
}
}
public bool isOpen
{
get
{
return window != null;
}
set
{
if(!value)
{
if(EditorApplication.SaveCurrentSceneIfUserWantsTo())
EditorApplication.Exit(0);
}
}
}
}
}
Note -- click here
http://answers.unity3d.com/questions/395202/editor-scripting-unity-4-problem-with-project-wind.html
for an update re Unity4 syntax.
Yeah it displays that warning even if the code is still there too for me: I have to reselect the same layout a couple of times and then all of the windows work again, each time I start Unity.
I updated the answer with support for the "main" window (so you can get and set windows.main.position). It's getting even further into undocumented territory though and doesn't work 100%.
Note also that I purposely didn't support the case of multiple windows of the same type (such as two Inspector windows being open at the same time) but I think it's possible using the FindAll function to access the windows separately.
Also, by the way, I suspect that full docking support might be possible by using reflection to mess around with instances of the UnityEditor.ContainerWindow and maybe SplitView classes enough, to add editor windows as children of container windows.
Thank you so much. This was exactly what I was looking for!
Wow, this totally saved me from pulling all my hair out! I've been trying to line up 3D elements with a GUI Layer background, but the locations of the 3D elements were changing between the Editor and the build. This was cuz my Game View wasn't showing my full 1080 x 1920 (portrait) resolution, even when maximized on a 1080p monitor. The Game View only goes up to 1903 pixels, likely due to the UI tabs at the top of the window.
But i was able to use this script to cheat the top position of the Game View window up to -19 pixels, so the UI tab is off-screen, which at least puts the window the right position so that now my game object positions in the editor match the fullscreen build. So now i can correctly position things in the editor!
Thanks much!
Answer by Eldoir · Jan 28, 2019 at 01:55 PM
Hi there,
As of Unity 2018.2.20f1, I can say that these types were renamed: - UnityEditor.ProjectWindow -> UnityEditor.ProjectBrowser - UnityEditor.HierarchyWindow -> UnityEditor.SceneHierarchyWindow - UnityEditor.LightmappingWindow -> UnityEditor.LightingWindow
The AssetServer ("UnityEditor.ASMainWindow") was removed.
Also, "get_mainView" becomes "m_RootView" in "UnityEditor.ContainerWindow" AND its BindingFlags become System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField | System.Reflection.BindingFlags.Instance.
Thanks @jspease for the great answer, which I updated to include the changes above:
using System;
using UnityEditor;
using UnityEngine;
public class WindowLayout
{
[MenuItem("Window/LAYOUT TEST", false, 99)]
public static void LayoutTestCommand()
{
// initialize window-specific title data and such (this can be cached)
WindowInfos windows = new WindowInfos();
// print some position
if (windows.game.isOpen)
Debug.Log("game window was open at " + windows.game.position);
else
Debug.Log("game window was closed");
// move some windows to various places, and open them if they aren't already open
windows.game.position = new Rect(100, 100, 300, 300); // left,top,width,height
windows.inspector.position = new Rect(400, 200, 200, 400);
windows.hierarchy.position = new Rect(600, 100, 100, 400);
windows.project.position = new Rect(700, 100, 100, 400);
windows.console.position = new Rect(200, 550, 600, 150);
// ensure a window is open without moving it from its previous position
windows.scene.isOpen = true;
// close a window if it was open
windows.animation.isOpen = false;
}
class WindowInfos
{
// note: some of this data might need to change a little between different versions of Unity
public WindowInfo scene = new WindowInfo("UnityEditor.SceneView", "Scene", "Window/Scene");
public WindowInfo game = new WindowInfo("UnityEditor.GameView", "Game", "Window/Game");
public WindowInfo inspector = new WindowInfo("UnityEditor.InspectorWindow", "Inspector", "Window/Inspector");
public WindowInfo hierarchy = new WindowInfo("UnityEditor.SceneHierarchyWindow", "Hierarchy", "Window/Hierarchy");
public WindowInfo project = new WindowInfo("UnityEditor.ProjectBrowser", "Project", "Window/Project");
public WindowInfo animation = new WindowInfo("UnityEditor.AnimationWindow", "Animation", "Window/Animation");
public WindowInfo profiler = new WindowInfo("UnityEditor.ProfilerWindow", "Profiler", "Window/Profiler");
public WindowInfo console = new WindowInfo("UnityEditor.ConsoleWindow", "Console", "Window/Console");
public WindowInfo navigation = new WindowInfo("UnityEditor.NavMeshEditorWindow", "Navigation", "Window/Navigation");
public WindowInfo occlusion = new WindowInfo("UnityEditor.OcclusionCullingWindow", "Occlusion", "Window/Occlusion Culling");
public WindowInfo lightmapping = new WindowInfo("UnityEditor.LightingWindow", "Lightmapping", "Window/Lightmapping");
//public WindowInfo assetServer = new WindowInfo("UnityEditor.ASMainWindow", "Server", "Window/Asset Server");
public WindowInfo assetStore = new WindowInfo("UnityEditor.AssetStoreWindow", "Asset Store", "Window/Asset Store");
public WindowInfo particle = new WindowInfo("UnityEditor.ParticleSystemWindow", "Particle Effect", "Window/Particle Effect");
public MainWindow main = new MainWindow();
}
class WindowInfo
{
string defaultTitle;
string menuPath;
Type type;
public WindowInfo(string typeName, string defaultTitle = null, string menuPath = null, System.Reflection.Assembly assembly = null)
{
this.defaultTitle = defaultTitle;
this.menuPath = menuPath;
if (assembly == null)
assembly = typeof(UnityEditor.EditorWindow).Assembly;
type = assembly.GetType(typeName);
if (type == null)
Debug.LogWarning("Unable to find type \"" + typeName + "\" in assembly \"" + assembly.GetName().Name + "\".\nYou might want to update the data in WindowInfos.");
}
public EditorWindow[] FindAll()
{
if (type == null)
return new EditorWindow[0];
return (EditorWindow[])(Resources.FindObjectsOfTypeAll(type));
}
public EditorWindow FindFirst()
{
foreach (EditorWindow window in FindAll())
return window;
return null;
}
public EditorWindow FindFirstOrCreate()
{
EditorWindow window = FindFirst();
if (window != null)
return window;
if (type == null)
return null;
if (menuPath != null && menuPath.Length != 0)
EditorApplication.ExecuteMenuItem(menuPath);
window = EditorWindow.GetWindow(type, false, defaultTitle);
return window;
}
// shortcut for setting/getting the position and size of the first window of this type.
// when setting the position, if the window doesn't exist it will also be created.
public Rect position
{
get
{
EditorWindow window = FindFirst();
if (window == null)
return new Rect(0, 0, 0, 0);
return window.position;
}
set
{
EditorWindow window = FindFirstOrCreate();
if (window != null)
window.position = value;
}
}
// shortcut for deciding if any windows of this type are open,
// or for opening/closing windows
public bool isOpen
{
get
{
return FindAll().Length != 0;
}
set
{
if (value)
FindFirstOrCreate();
else
foreach (EditorWindow window in FindAll())
window.Close();
}
}
}
// experimental support for getting at the main Unity window
class MainWindow
{
Type type;
UnityEngine.Object window;
public MainWindow()
{
type = typeof(UnityEditor.EditorWindow).Assembly.GetType("UnityEditor.ContainerWindow");
if (type != null)
{
foreach (UnityEngine.Object w in Resources.FindObjectsOfTypeAll(type))
{
object parent = type.InvokeMember("m_RootView", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField | System.Reflection.BindingFlags.Instance, null, w, null);
if (parent != null && parent.GetType().Name.Contains("MainWindow"))
{
window = w;
break;
}
}
}
if (window == null)
Debug.LogWarning("Unable to find main window.\nMaybe you'll need to update the MainWindow constructor for your version of Unity.");
}
public Rect position
{
get
{
if (window == null)
return new Rect(0, 0, 0, 0);
return (Rect)type.InvokeMember("get_position", System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Instance, null, window, null);
}
set
{
if (window != null)
type.InvokeMember("set_position", System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Instance, null, window, new object[] { value });
}
}
public bool isOpen
{
get
{
return window != null;
}
set
{
if (!value)
{
if (UnityEditor.SceneManagement.EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo())
EditorApplication.Exit(0);
}
}
}
}
}
Your answer
Follow this Question
Related Questions
Show entire script name in editor. 1 Answer
The name `StaticOcclusionCullingMode' does not exist in the current context 1 Answer
How to make a editor script that sets the values of serialized variables in the editor 1 Answer
Remove or disable an EditorGUILayout.PropertyField 0 Answers
SearchableEditorWindow 2 Answers