2
\$\begingroup\$

I want to create an instance of Editor Window by using CreateInstance or GetWindow. And I want to place it in center of the screen (or center of Unity).

I didn't find any methods in Unity Engine that could help me. Do they exist?

How can I do it within the script?

Note: I mean: how can I find out what is the user's screen resolution? Which method can give me that?

P.S. Screen.width/Screen.height - is not what I need. Because it refers to the Game View's window, not Unity.exe Editor's size and not monitor Screen

\$\endgroup\$
2
\$\begingroup\$

There is a way of obtaining Unity Editor's main window. From there you can just obtain window size and set your custom editor window position :). I will just leave you the link.

[In case the link is broken]

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Linq;

 public static class Extensions
 {
 public static System.Type[] GetAllDerivedTypes(this System.AppDomain aAppDomain, System.Type aType)
 {
     var result = new List<System.Type>();
     var assemblies = aAppDomain.GetAssemblies();
     foreach (var assembly in assemblies)
     {
         var types = assembly.GetTypes();
         foreach (var type in types)
         {
             if (type.IsSubclassOf(aType))
                 result.Add(type);
         }
     }
     return result.ToArray();
 }

 public static Rect GetEditorMainWindowPos()
 {
     var containerWinType = System.AppDomain.CurrentDomain.GetAllDerivedTypes(typeof(ScriptableObject)).Where(t => t.Name == "ContainerWindow").FirstOrDefault();
     if (containerWinType == null)
         throw new System.MissingMemberException("Can't find internal type ContainerWindow. Maybe something has changed inside Unity");
     var showModeField = containerWinType.GetField("m_ShowMode", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
     var positionProperty = containerWinType.GetProperty("position", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
     if (showModeField == null || positionProperty == null)
         throw new System.MissingFieldException("Can't find internal fields 'm_ShowMode' or 'position'. Maybe something has changed inside Unity");
     var windows = Resources.FindObjectsOfTypeAll(containerWinType);
     foreach (var win in windows)
     {
         var showmode = (int)showModeField.GetValue(win);
         if (showmode == 4) // main window
         {
             var pos = (Rect)positionProperty.GetValue(win, null);
             return pos;
         }
     }
     throw new System.NotSupportedException("Can't find internal main window. Maybe something has changed inside Unity");
 }

 public static void CenterOnMainWin(this UnityEditor.EditorWindow aWin)
 {
     var main = GetEditorMainWindowPos();
     var pos = aWin.position;
     float w = (main.width - pos.width)*0.5f;
     float h = (main.height - pos.height)*0.5f;
     pos.x = main.x + w;
     pos.y = main.y + h;
     aWin.position = pos;
     }
 }

This is a js script, so if you are using Unity 2017.x you should probably translate it to c#(which shouldn't be too difficult since the syntax is pretty much the same). In this script there is also a function that let's you position every editor window in the center of the Main Unity window. All we have done here was using Extensions.GetEditorMainWindowPos() to get back the rect of the main window in screen coordinates.

\$\endgroup\$
  • 1
    \$\begingroup\$ This answer would be better if if included the method described at the link. Without this, if the lined page ever goes down, a user coming to this thread for help will get only "there is a way.... but I won't tell you what it is!" ;) \$\endgroup\$ – DMGregory Dec 2 '17 at 23:58
  • 1
    \$\begingroup\$ Oh, sure i'll do it immediately :D \$\endgroup\$ – user100681 Dec 3 '17 at 8:54
0
\$\begingroup\$

Even though I've already found the code that were presented by @GabrieleVierti in his answer (I think it's too big and massive), I found and preferred to use Screen.currentResolution, which returns the current screen resolution.

For now it's fine for me.

\$\endgroup\$
-1
\$\begingroup\$

EditorWindow - How do I center a Editor Window?

To set position your editor windows instance you need to use windows.positon.

var window = new MyWindow(); window.position = new Rect(Screen.Width/2,Screen.Height/2,599,299);

enter image description here

using UnityEngine;
using UnityEditor;
using System.Collections;

class MyWindow : EditorWindow
{
    string myString = "Hello World";
    bool groupEnabled;
    bool myBool = true;
    float myFloat = 1.23f;

    // Add menu item named "My Window" to the Window menu
    [MenuItem("Window/My Window")]
    public static void ShowWindow()
    {
        //Show existing window instance. If one doesn't exist, make one.
        var window = EditorWindow.GetWindow(typeof(MyWindow));
        window.position = new Rect(Screen.width / 2, Screen.height / 2, 599, 299);
    }

    void OnGUI()
    {
        GUILayout.Label("Base Settings", EditorStyles.boldLabel);
        myString = EditorGUILayout.TextField("Text Field", myString);

        groupEnabled = EditorGUILayout.BeginToggleGroup("Optional Settings", groupEnabled);
        myBool = EditorGUILayout.Toggle("Toggle", myBool);
        myFloat = EditorGUILayout.Slider("Slider", myFloat, -3, 3);
        EditorGUILayout.EndToggleGroup();
    }
}

\$\endgroup\$
  • \$\begingroup\$ To set position your editor windows instance you need to use windows.positon. - I know it, thanks :) But the main question is what class/methods will help me do that? I mean... how to know actual user screen resolution? wich method can give me that? And I wrote that Screen.width/Screen.height - is not what I need. Because it refers to the Game View's window, not Unity.exe Editor's size and not monitor Screen.... that is the key moment.....so, for example window.position = new Rect(Screen.width / 2, Screen.height / 2, 100, 299) will not set the window in the center at all))) \$\endgroup\$ – Alexey Shimansky Jun 22 '17 at 18:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.