How to resize a floating GameView in the UnityEditor?

I am trying to create an editor script that will resize the game view in the unity editor, so that I can record frames in a specified resolution. In other words (using UnityEditor):

EditorWindow view = EditorWindow.GetWindow<GameView>();
view.position = new Rect( 20, 45, 512, 512 );

This will give you the message “The type or namespace name `GameView’ could not be found”.

After a couple of frustrating hours I managed to find a reproducible – but awkward – way of getting the GameView. Close the current GameView manually, create a new floating GameView and then run the code:

EditorWindow view = EditorWindow.GetWindow<EditorWindow>();
Debug.Log( view.GetType() );

This will print the type of “UnityEditor.GameView” in the console. It seams that this will return the last created EditorWindow.

But that solutions sucks. I also tried to create a new GameView by executing:

EditorApplication.ExecuteMenuItem( "Window/Game" );

But for that to work I first need to close the current GameView, so that takes me nowhere.

I don’t know why the GameView is not exposed. If anyone can give me good reason for that I would appreciate that as well, because since Unity was created by coding Gods there must be a good reason … right?

~ce

Look at this amazing answer by jspease