x


Game window size from editor window in editor mode

Hi all,

there's several variants of this question floating around, but this one, I haven't found a solution nor even an idea of how to solve.

Basically, from an editor window, in editor mode, I have the need to retrieve the 'game window' current width and height. You might already know in fact that when you address screen.width or height from an editor window (the one created by an editor script), the values you receive are the dimensions of the actual editor window, and not of the game window!

I can't possibly think that this issue can't be solved in editor mode, from an editor window, so please help me understand how to retrieve the game-window pixel dimensions in edit mode.

Note: for a series of design requisites, I can't simply set the aspect to standalone, and a predetermined X-Y value, and stick with that, basically because these necessarily vary from development seat to seat.

Thanks.

more ▼

asked Oct 25 '11 at 06:56 AM

roamcel gravatar image

roamcel
1.2k 37 40 44

Would love to see an answer to this one :P

Nov 15 '11 at 11:36 AM Izitmee
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Unfortunately the GameView class is an internal class so the only way would be to use reflection to make your way down to the actual GameView window.

Here's a similar case on how to access internal classes via reflection:

http://answers.unity3d.com/questions/129694/can-you-turn-off-auto-keyframe-in-the-animation-wi.html

But be careful! Those classes are internal and they can change them at any time. It's something you actually shouldn't use if you can avoid it.

edit

I've written a little helper function which will return the size of the main GameView:

// C#
public static Vector2 GetMainGameViewSize()
{
    System.Type T = System.Type.GetType("UnityEditor.GameView,UnityEditor");
    System.Reflection.MethodInfo GetSizeOfMainGameView = T.GetMethod("GetSizeOfMainGameView",System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
    System.Object Res = GetSizeOfMainGameView.Invoke(null,null);
    return (Vector2)Res;
}
more ▼

answered Dec 07 '11 at 12:52 PM

Bunny83 gravatar image

Bunny83
46.8k 12 50 210

This is a great solution, thanks a lot. Additional value in your heads up regarding the internal classes usage.

Dec 07 '11 at 03:54 PM roamcel

Hi @bunny83 is there a way through which i can set the resolution back..

Jun 26 '12 at 12:17 PM flamy

@flamy: Set it back? to what? I don't change the resolution. This will just determine the size of the first gameview. It's generally a bad idea to set the size of any editor window via code, since this will most likely undock the window and break your layout. If you want to do this because you have your window already undocked, you can set the size and position for the gameview the same way as for any other Editorwindow. All you need is a reference to the gameview:

untested:

// C#
public static EditorWindow GetMainGameView()
{
    System.Type T = System.Type.GetType("UnityEditor.GameView,UnityEditor");
    System.Reflection.MethodInfo GetMainGameView = T.GetMethod("GetMainGameView",System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
    System.Object Res = GetMainGameView.Invoke(null,null);
    return (EditorWindow)Res;
}

With this you should be able to use any Editorwindow properties like the position:

Rect R = GetMainGameView().position;
R.width = 800;
R.height = 600;
GetMainGameView().position = R;

Note that this will set the window's position, the actual view size could be a bit smaller due to the window border.

There is no build in function to set it to a certain resolution. There is the internal function GetGameViewRect which calculates the view size depending on the window size, but not reverse ;)

Jun 27 '12 at 03:38 AM Bunny83
(comments are locked)
10|3000 characters needed characters left
using UnityEngine;
using UnityEditor;

public class AShimSetGameSizeWindow: EditorWindow {

    private Vector2 _size = new Vector2(800, 600);

    [MenuItem ("Window/Set Game Size...")]
    static void Init () {
       AShimSetGameSizeWindow window = (AShimSetGameSizeWindow)(EditorWindow.GetWindow(typeof(AShimSetGameSizeWindow)));
    } // Init()

    public static EditorWindow GetMainGameView() {
       System.Type T = System.Type.GetType("UnityEditor.GameView,UnityEditor");
       System.Reflection.MethodInfo GetMainGameView = T.GetMethod("GetMainGameView",System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
       System.Object Res = GetMainGameView.Invoke(null,null);
       return (EditorWindow)Res;
    } // GetMainGameView()

    void OnGUI () {
       _size.x = EditorGUILayout.IntField("X", (int)_size.x);
       _size.y = EditorGUILayout.IntField("Y", (int)_size.y);
       if (GUILayout.Button("Set")) {
         EditorWindow gameView = GetMainGameView();
         Rect pos = gameView.position;
         pos.y = pos.y - 0;
         pos.width = _size.x;
         pos.height = _size.y + 17;
         gameView.position = pos;
       }
    } // OnGUI()

} // class AShimSetGameSizeWindow
more ▼

answered Sep 12 '12 at 12:33 PM

AShim.3D gravatar image

AShim.3D
0

(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:

x3418
x1728
x823
x208
x20

asked: Oct 25 '11 at 06:56 AM

Seen: 4192 times

Last Updated: Sep 12 '12 at 12:33 PM