Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
unity editor script: UnityEditor GameView automatically move to "Rift DK2 Extend Display"
using UnityEngine;
using UnityEditor;
//Version 0.21 | twitter:@izm update for DK2
//Version 0.2 | s.b.Newsom Edition
//Source from
//Modified by seieibob for use at the Virtual Environment and Multimodal Interaction Lab at the University of Maine.
//Use however you'd like!
//Modified by sbNewsom. Like it is said above, use as you like! If you're interested in my work, check out:
/// <summary>
/// Displays a popup window that undocks, repositions and resizes the game window according to
/// what is specified by the user in the popup. Offsets are applied to ensure screen borders are not shown.
/// </summary>
public class GameWindowMover: EditorWindow {
//The size of the toolbar above the game view, excluding the OS border.
private int tabHeight = 22;
private bool toggle = true;
//Get the size of the window borders. Changes depending on the OS.
//Windows settings
private int osBorderWidth = 5;
//Mac settings (untested)
private int osBorderWidth = 0; //OSX windows are borderless.
//Linux / other platform; sizes change depending on the variant you're running
private int osBorderWidth = 5;
//default setting
private static Vector2 _gameSize = new Vector2(1530, 800);
private static Vector2 _gamePosition = new Vector2(2050, 50);
//Desired window resolution
public Vector2 gameSize = new Vector2(_gameSize.x,_gameSize.y);
//Desired window position
public Vector2 gamePosition =new Vector2(_gamePosition.x,_gamePosition.y);
//Tells the script to use the default resolution specified in the player settings.
private bool usePlayerSettingsResolution = false;
//For those that duplicate screen
private bool useDesktopResolution = false;
//Shows the popup
[MenuItem ("Window/Rift VR Game Mode")]
static void OpenPopup() {
GameWindowMover window = (GameWindowMover)(EditorWindow.GetWindow(typeof(GameWindowMover)));
//Set popup window properties
Vector2 popupSize = new Vector2(300, 140);
//When minSize and maxSize are the same, no OS border is applied to the window.
window.minSize = popupSize;
window.maxSize = popupSize;
window.title = "RiftMode";
//Returns the current game view as an EditorWindow object.
public static EditorWindow GetMainGameView(){
//Creates a game window. Only works if there isn't one already.
System.Type T = System.Type.GetType("UnityEditor.GameView,UnityEditor");
System.Reflection.MethodInfo GetMainGameView = T.GetMethod("GetMainGameView",System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
System.Object Res = GetMainGameView.Invoke(null,null);
return (EditorWindow)Res;
void OnGUI(){
gameSize = new Vector2(Screen.currentResolution.width, Screen.currentResolution.height);
gameSize = EditorGUILayout.Vector2Field("Rift Display Size:", gameSize);
gamePosition = EditorGUILayout.Vector2Field("Rift Display Position:", gamePosition);
GUILayout.Label("Rift Game Mode is now activated. ");
GUILayout.Label("Don't close this panel to keep script in effect.");
void Update() {
toggle = true;
} else {
toggle = false;
void MoveGameWindow(){
EditorWindow gameView = GetMainGameView();
gameView.title = "Game (Oculus Rift)";
//When minSize and maxSize are the same, no OS border is applied to the window.
gameView.minSize = new Vector2(gameSize.x, gameSize.y + tabHeight - osBorderWidth);
gameView.maxSize = gameView.minSize;
Rect newPos = new Rect(gamePosition.x, gamePosition.y - tabHeight, gameSize.x, gameSize.y + tabHeight - osBorderWidth);
gameView.position = newPos;
void CloseGameWindow(){
EditorWindow gameView = GetMainGameView();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment