Advertisement
Jellybit

Oculus Rift test window for Unity

Jan 20th, 2014
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.78 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3.  
  4. //Version 0.2 | s.b.Newsom Edition
  5.  
  6. //Source from http://answers.unity3d.com/questions/179775/game-window-size-from-editor-window-in-editor-mode.html
  7. //Modified by seieibob for use at the Virtual Environment and Multimodal Interaction Lab at the University of Maine.
  8. //Use however you'd like!
  9.  
  10. //Modified by sbNewsom. Like it is said above, use as you like! If you're interested in my work, check out:
  11. //http://www.sbnewsom.com
  12.  
  13. /// <summary>
  14. /// Displays a popup window that undocks, repositions and resizes the game window according to
  15. /// what is specified by the user in the popup. Offsets are applied to ensure screen borders are not shown.
  16. /// </summary>
  17. public class GameWindowMover: EditorWindow {
  18.    
  19.     //The size of the toolbar above the game view, excluding the OS border.
  20.     private int tabHeight = 22;
  21.    
  22.     private bool toggle = true;
  23.    
  24.     //Get the size of the window borders. Changes depending on the OS.
  25.     #if UNITY_STANDALONE_WIN
  26.     //Windows settings
  27.     private int osBorderWidth = 5;
  28.     #elif UNITY_STANDALONE_OSX
  29.     //Mac settings (untested)
  30.     private int osBorderWidth = 0; //OSX windows are borderless.
  31.     #else
  32.     //Linux / other platform; sizes change depending on the variant you're running
  33.     private int osBorderWidth = 5;
  34.     #endif
  35.    
  36.     //Desired window resolution
  37.     private Vector2 gameSize = new Vector2(1280, 800);
  38.     //Desired window position
  39.     private Vector2 gamePosition = new Vector2(0, 0);
  40.    
  41.     //Tells the script to use the default resolution specified in the player settings.
  42.     private bool usePlayerSettingsResolution = false;
  43.    
  44.     //For those that duplicate screen
  45.     private bool useDesktopResolution = false;
  46.  
  47.     //Shows the popup
  48.     [MenuItem ("Window/Rift VR Game Mode")]
  49.     static void OpenPopup() {
  50.         GameWindowMover window = (GameWindowMover)(EditorWindow.GetWindow(typeof(GameWindowMover)));
  51.         //Set popup window properties
  52.         Vector2 popupSize = new Vector2(300, 140);
  53.         //When minSize and maxSize are the same, no OS border is applied to the window.
  54.         window.minSize = popupSize;
  55.         window.maxSize = popupSize;
  56.         window.title = "RiftMode";
  57.         window.ShowPopup();
  58.     }
  59.  
  60.     //Returns the current game view as an EditorWindow object.
  61.     public static EditorWindow GetMainGameView(){
  62.         //Creates a game window. Only works if there isn't one already.
  63.         EditorApplication.ExecuteMenuItem("Window/Game");
  64.        
  65.         System.Type T = System.Type.GetType("UnityEditor.GameView,UnityEditor");
  66.         System.Reflection.MethodInfo GetMainGameView = T.GetMethod("GetMainGameView",System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
  67.         System.Object Res = GetMainGameView.Invoke(null,null);
  68.         return (EditorWindow)Res;
  69.     }
  70.  
  71.     void OnGUI(){
  72.        
  73.         EditorGUILayout.Space();
  74.        
  75. if(useDesktopResolution){
  76.             gameSize = new Vector2(Screen.currentResolution.width, Screen.currentResolution.height);
  77.         }
  78.  
  79.     GUILayout.Label("Rift Game Mode is now activated. ");
  80.        
  81.         GUILayout.Label("Don't close this panel to keep script in effect.");
  82.     }
  83.     void Update() {
  84.         if(Application.isPlaying){
  85.             MoveGameWindow();
  86.             toggle = true;
  87.         } else {
  88.             if(toggle){
  89.                 CloseGameWindow();
  90.                 toggle = false;
  91.             }
  92.         }
  93.        
  94.  
  95.        
  96.     }
  97.    
  98.     void MoveGameWindow(){
  99.         EditorWindow gameView = GetMainGameView();
  100.         gameView.title = "Game (Oculus Rift)";
  101.         //When minSize and maxSize are the same, no OS border is applied to the window.
  102.         gameView.minSize = new Vector2(gameSize.x, gameSize.y + tabHeight - osBorderWidth);
  103.         gameView.maxSize = gameView.minSize;
  104.         Rect newPos = new Rect(gamePosition.x, gamePosition.y - tabHeight, gameSize.x, gameSize.y + tabHeight - osBorderWidth);
  105.         gameView.position = newPos;
  106.         gameView.ShowPopup();  
  107.     }
  108.    
  109.     void CloseGameWindow(){
  110.         EditorWindow gameView = GetMainGameView();
  111.         gameView.Close();  
  112.     }
  113.  
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement