x


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 ...

alt text

... 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.

crap.jpg (47.8 kB)
more ▼

asked Jun 14 '12 at 03:50 PM

Fattie gravatar image

Fattie
18.9k 57 86 146

@jspease .. if you happen to see this message,

it appears something was changed in Unity4, and "Project" doesn't work. I'm really sorry to bother you! Only you know what you're doing on this, cheers! :)

http://answers.unity3d.com/questions/395202/editor-scripting-unity-4-problem-with-project-wind.html

Feb 08 at 09:16 AM Fattie
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Jun 15 '12 at 09:52 AM

jspease gravatar image

jspease
569 1 3 6

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.

Jun 15 '12 at 11:05 AM whydoidoit

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.

Jun 15 '12 at 07:28 PM jspease

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.

Jun 15 '12 at 07:34 PM jspease

Thank you so much. This was exactly what I was looking for!

Jun 18 '12 at 12:48 PM CarlEmail

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!

Feb 04 at 05:49 AM madcalf
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1663
x6

asked: Jun 14 '12 at 03:50 PM

Seen: 1125 times

Last Updated: Feb 11 at 07:33 PM