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.
@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.
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:
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
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.
Would love to see an answer to this one :P
– DemigiantWhy don't you use [Handles.GetMainGameViewSize][1]? [1]: http://docs.unity3d.com/ScriptReference/Handles.GetMainGameViewSize.html
– exawon@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.
– Wolfram