cancel
Showing results for 
Search instead for 
Did you mean: 

Small utility script - CloseSceneViewOnPlay

Dhrog
Honored Guest
Here is a small utility script/asset package for the Unity Editor. It makes life a bit easier on multi monitor setups.

The script moves your Gamewindow to the rift and closes the scene view when playing a scene in the editor.
Stopping the scene restores a window layout you can save in an additional menu.

The mode can be turned off or on in the /windows/Oculus menu.

Very helpful to save some render performance and to avoid display burn in while developing in extended mode.
I also put some tips into the comments that helped to reduce judder, which plagued me a lot on the DK2 in the Unity Editor.


//Utility script for the DK2
//This script adds an Oculus menu to the Windows Menu in the Unity Editor
//Save Layout -> Saves your current layout to a Default.wlt file
//Turn OVR Mode On ->
//In this mode the scene view window closes after pressing play,
//while the game view will be moved to the rift display.
//After exiting the playmode your stored window layout will be loaded.
//Turn OVR Mode Off -> the windows won't be moved.
//I already have some burn in effects on my rift, this script should help to minimize burn in if you
//set you desktop backdrop to black (... I guess 😄 )
//While it adds some comfort, it also helps to increase rendering performance in the Unity Editor to avoid judder.
//The assetpackage comes with default settings for my monitor setup. You need to change them!
//If you find bugs... fix 'em :D, Pepe 2014

using UnityEngine;
using UnityEditor;
using System.Reflection;


[InitializeOnLoad]
public class CloseSceneViewOnPlay {

//Game Window position - You must change those screen coords to match your screen setup
static private Vector2 gamePosition = new Vector2(3040, 0);
//Game Window resolution
static private Vector2 gameSize = new Vector2(1920, 1080);
//Path to save layout setting
static private string wltPath = "Assets/Scripts/Editor/Layouts/Default.wlt";

[MenuItem("Window/Oculus/Save Layout")]
static void SaveLayoutHack() {
// Saving the current layout to an asset
LayoutUtility.SaveLayoutToAsset(wltPath);
}

[MenuItem("Window/Oculus/Load Layout")]
static void LoadLayoutHack() {
// Loading layout from stored asset
LayoutUtility.LoadLayoutFromAsset(wltPath);
}

[MenuItem("Window/Oculus/Turn OVR Mode On")]
static void TurnOVROn() {
//Store mode in player prefs
PlayerPrefs.SetInt("MoveWindow",1);
}

[MenuItem("Window/Oculus/Turn OVR Mode Off")]
static void TurnOVROff() {
PlayerPrefs.SetInt("MoveWindow",0);
}

static CloseSceneViewOnPlay()
{
EditorApplication.playmodeStateChanged += HandleOnPlayModeChanged;
}

//Returns the current game view as an EditorWindow object.
private 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;
}

private static EditorWindow getEditorWindowOfTypeStr(string type)
{
EditorWindow[] allWindows = Resources.FindObjectsOfTypeAll(typeof(EditorWindow)) as EditorWindow[];
EditorWindow requestedWindow = null;
foreach (EditorWindow window in allWindows)
{
if (window.GetType().ToString() == type)
{
requestedWindow = window;
break;
}
}
return requestedWindow;
}

private static void MoveGameWindow(){

int tabHeight = 22;
int osBorderWidth = 5;
EditorWindow gameView = GetMainGameView();
gameView.title = "Game Oculus";
//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 - 5);
gameView.position = newPos;
gameView.ShowPopup();
gameView.Focus();
}

private static void HandleOnPlayModeChanged() // This method is run whenever the playmode state is changed.
{
//I tested a lot of things to make the experience for the DK2 as smooth as it was with the DK1
//while working in the Unity Editor.
//Here are some things I found out... if you experience judder you can test if those tips help.
//VSync of 75hz should be turned on in the gfx driver!
//Aero and Windows Compositing should be "enabled"! There are some tips out there to turn it off!
//Don't turn it off. At least on my machine (Win7 - x64, GTX 680 + 3 Screens + Rift) turning
//one of those options off results in judder while working in the Unity editor.
//I run my three screens in 1520x855@75hz. This is the maximum resolution causing no out of sync.
//All screens in a multi monitor setup should run in 75hz (or set the rift to primary)
//Timewarp should be turned off (Causes judder even in my builds). Take a look at the profiler if occlusion culling is really faster.
//If there's no performance benefit, turn it of for both OVR cams in your project.
//OC killed performance on my current projet (60hz vs. 250hz) when I had activated the oc checkboxes.
//Ajust your HDMI color range to full (0-255) in the NVidia Panel.

Application.targetFrameRate = 80; //Never get below 75fps!!!!

if (PlayerPrefs.GetInt("MoveWindow") == 1) {
try {MoveGameWindow();} catch {}
if (EditorApplication.isPlaying)
{
EditorWindow GetWindowEx = EditorWindow.GetWindow<SceneView>("Scene",false,typeof(SceneView) );
GetWindowEx.Focus();
GetWindowEx.Close ();
}
if (!EditorApplication.isPlaying )
{
LayoutUtility.LoadLayoutFromAsset("Assets/Scripts/Editor/Layouts/Default.wlt");
}
}
}

}



You can download the asset package here:
https://www.dropbox.com/s/chfbhg8r8qmvtze/CloseSceneViewOnPlay.unitypackage?dl=0

Have fun,

Pepe
1 REPLY 1

cybereality
Grand Champion
Thanks for sharing this.