- Home /
The question is answered, right answer was accepted
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.
Why don't you use Handles.Get$$anonymous$$ainGameViewSize?
@exawon because that API interface was not available back in 2011 when this question was asked. Handles.Get$$anonymous$$ainGameViewSize was introduced in Unity 4.x, I believe.
Answer by Bunny83 · Dec 07, 2011 at 12:52 PM
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..
@flamy: Set it back? to what? I don't change the resolution. This will just deter$$anonymous$$e 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 Get$$anonymous$$ainGameView()
{
System.Type T = System.Type.GetType("UnityEditor.GameView,UnityEditor");
System.Reflection.$$anonymous$$ethodInfo Get$$anonymous$$ainGameView = T.Get$$anonymous$$ethod("Get$$anonymous$$ainGameView",System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
System.Object Res = Get$$anonymous$$ainGameView.Invoke(null,null);
return (EditorWindow)Res;
}
With this you should be able to use any Editorwindow properties like the position:
Rect R = Get$$anonymous$$ainGameView().position;
R.width = 800;
R.height = 600;
Get$$anonymous$$ainGameView().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 ;)
Thanks. This just helped me recovering from a Unity bug (seen in 3.5.7f6), where Application.CaptureScreenshot() will create shifted snapshots, leaving a black line at the bottom/left and cropping 1 pixel on the top/right. Application.CaptureScreenshot() only behaves correctly if the width is even and the height is odd (even if the Game window is larger than your capture size!), so I used your method to ensure that.
NOTE: for some reason, when assigning to Get$$anonymous$$ainGameView().position, you always need to subtract an offset of 5 from the Rect.y value you read from it. If you reassign the same value, the window moves around.
As @exawon noticed, with Unity 4.x you should be able to use the "official" API call Handles.Get$$anonymous$$ainGameViewSize, ins$$anonymous$$d of this workaround. Didn't test it, though.