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.

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;
}

Since Unity 2022.2, you can now use the GetRenderingResolution method of the PlayModeWindow utility class.

Source code: UnityCsReference/Editor/Mono/PlayModeView/PlayModeWindow.cs at master · Unity-Technologies/UnityCsReference · GitHub

UnityEditor.PlayModeWindow.GetRenderingResolution(out uint width, out uint height);
Debug.Log($"{width}x{height}");

However, I don’t know why they used the uint type instead of the int type.