Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
5
Question by Fattie · Jun 14, 2012 at 03:50 PM · editoreditor-classes

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)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
10
Best Answer

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.

Comment
Add comment · Show 14 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · Jun 15, 2012 at 11:05 AM 1
Share

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.

avatar image jspease · Jun 15, 2012 at 07:28 PM 1
Share

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.

avatar image jspease · Jun 15, 2012 at 07:34 PM 1
Share

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.

avatar image cecarlsen · Jun 18, 2012 at 12:48 PM 1
Share

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

avatar image madcalf · Feb 04, 2013 at 05:49 AM 1
Share

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!

Show more comments
avatar image
0

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);
                 }
             }
         }
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges