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.

Would love to see an answer to this one :P

Why don't you use [Handles.GetMainGameViewSize][1]? [1]: http://docs.unity3d.com/ScriptReference/Handles.GetMainGameViewSize.html

@exawon because that API interface was not available back in 2011 when this question was asked. Handles.GetMainGameViewSize was introduced in Unity 4.x, I believe.

2 Answers

2

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

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

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

Thanks @bunny73 It really helped Javascript version if someome needs it : public static function GetMainGameViewSize():Vector2 { var T : System.Type; T = System.Type.GetType("UnityEditor.GameView,UnityEditor"); var GetSizeOfMainGameView : System.Reflection.MethodInfo; GetSizeOfMainGameView = T.GetMethod("GetSizeOfMainGameView",System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static); var Res : System.Object; Res = GetSizeOfMainGameView.Invoke(null,null); return Res; }

As @exawon noticed, with Unity 4.x you should be able to use the "official" API call Handles.GetMainGameViewSize, instead of this workaround. Didn't test it, though.

I found a way to parse it out from UnityEditor.UnityStats... #if UNITY_EDITOR string[] resWidthAndHeight = UnityEditor.UnityStats.screenRes.Split(new char[]{'x'}); int screenWidth = System.Convert.ToInt32(resWidthAndHeight[0]); int screenHeight = System.Convert.ToInt32(resWidthAndHeight[1]); Debug.Log("Size: " + UnityEditor.UnityStats.screenRes); // 960x640 #endif

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.

It’s worth noting that if the actual window resolution is smaller than the resolution selected in the dropdown, it will return the window resolution and not the one specified in the dropdown. I wanted to use the dropdown to set the resolution for very large screenshots, but this doesn’t cut it.