SarahNorthway

FullscreenPlayMode

Jun 1st, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.39 KB | None | 0 0
  1. using UnityEditor;
  2. using UnityEngine;
  3. using System.Collections;
  4. using System.Reflection;
  5. using System;
  6.  
  7. /// <summary>
  8. /// From https://www.reddit.com/r/Unity3D/comments/2lymim/full_full_screen_on_play_script_freebie_for/
  9. /// and https://answers.unity.com/questions/956123/add-and-select-game-view-resolution.html
  10. /// </summary>
  11. [InitializeOnLoad]
  12. public class FullscreenPlayMode : MonoBehaviour {
  13.     public static bool fullscreenEnabled = true;
  14.  
  15.     //The size of the toolbar above the game view, excluding the OS border.
  16.     private static int tabHeight = 22;
  17.     static object gameViewSizesInstance;
  18.     static MethodInfo getGroup;
  19.  
  20.     static FullscreenPlayMode() {
  21.         if (!fullscreenEnabled) return;
  22.         EditorApplication.playModeStateChanged -= CheckPlayModeState;
  23.         EditorApplication.playModeStateChanged += CheckPlayModeState;
  24.  
  25.         var sizesType = typeof(Editor).Assembly.GetType("UnityEditor.GameViewSizes");
  26.         var singleType = typeof(ScriptableSingleton<>).MakeGenericType(sizesType);
  27.         var instanceProp = singleType.GetProperty("instance");
  28.         getGroup = sizesType.GetMethod("GetGroup");
  29.         gameViewSizesInstance = instanceProp.GetValue(null, null);
  30.     }
  31.  
  32.     /// <summary>
  33.     /// Only called with EnteredPlayMode and EnteredEditMode, never Exit for some reason in 2017.3.0f3
  34.     /// </summary>
  35.     static void CheckPlayModeState(PlayModeStateChange state) {
  36.         if (!fullscreenEnabled) return;
  37.         if (state == PlayModeStateChange.EnteredPlayMode) {
  38.             FullScreenGameWindow();
  39.         } else if (state == PlayModeStateChange.EnteredEditMode) {
  40.             CloseGameWindow();
  41.         }
  42.     }
  43.  
  44.     static EditorWindow GetMainGameView() {
  45.         EditorApplication.ExecuteMenuItem("Window/Game");
  46.  
  47.         System.Type T = System.Type.GetType("UnityEditor.GameView,UnityEditor");
  48.         System.Reflection.MethodInfo GetMainGameView = T.GetMethod("GetMainGameView", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
  49.         System.Object Res = GetMainGameView.Invoke(null, null);
  50.         return (EditorWindow)Res;
  51.     }
  52.  
  53.     static void FullScreenGameWindow() {
  54.         EditorWindow gameView = GetMainGameView();
  55.  
  56.         //gameView.title = "Game (Stereo)";
  57.         Rect newPos = new Rect(0, 0 - tabHeight, Screen.currentResolution.width, Screen.currentResolution.height + tabHeight);
  58.  
  59.         // move down to second monitor beneath main one
  60.         newPos.y += 1080;
  61.  
  62.         gameView.position = newPos;
  63.         gameView.minSize = new Vector2(Screen.currentResolution.width, Screen.currentResolution.height + tabHeight);
  64.         gameView.maxSize = gameView.minSize;
  65.         gameView.position = newPos;
  66.  
  67.         // since camera may have moved, re-align the scene view to it
  68.         NWUtils.RunLater(NWUtils.AlignSceneToCamera, 0.5f, true);
  69.     }
  70.  
  71.     static void CloseGameWindow() {
  72.         EditorWindow gameView = GetMainGameView();
  73.         gameView.Close();
  74.  
  75.         Type gameViewType = Type.GetType("UnityEditor.GameView,UnityEditor.dll");
  76.         Type[] desiredDockNextTo = new Type[] { typeof(UnityEditor.SceneView) };
  77.  
  78.         MethodInfo getWindow = typeof(EditorWindow).GetMethod("GetWindow", new Type[] { typeof(string), typeof(bool), desiredDockNextTo.GetType() });
  79.         MethodInfo getWindowGameView = getWindow.MakeGenericMethod(gameViewType);
  80.         getWindowGameView.Invoke(null, new object[] { "Game", false, desiredDockNextTo });
  81.  
  82.         // set the game screen size back to 16:9
  83.         SetWidescreen();
  84.  
  85.         // set focus back on Scene tab
  86.         EditorWindow.FocusWindowIfItsOpen(typeof(UnityEditor.SceneView));
  87.  
  88.         // since camera may have moved, re-align the scene view to it
  89.         NWUtils.AlignSceneToCamera();
  90.     }
  91.  
  92.     public static void MaximizeGameWindow() {
  93.         var assembly = Assembly.GetAssembly(typeof(Editor));
  94.         var type = assembly.GetType("UnityEditor.GameView");
  95.         var property = type.GetProperty("maximized");
  96.         var window = EditorWindow.GetWindow(type);
  97.         var maximized = (bool)property.GetValue(window, null);
  98.         property.SetValue(window, (object)(!maximized), null);
  99.     }
  100.  
  101.     public static void SetWidescreen() {
  102.         SetSize(FindSize(GameViewSizeGroupType.Standalone, "16:9"));
  103.     }
  104.  
  105.     public static void SetSize(int index) {
  106.         var gvWndType = typeof(Editor).Assembly.GetType("UnityEditor.GameView");
  107.         var selectedSizeIndexProp = gvWndType.GetProperty("selectedSizeIndex",
  108.                 BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
  109.         var gvWnd = EditorWindow.GetWindow(gvWndType);
  110.         selectedSizeIndexProp.SetValue(gvWnd, index, null);
  111.     }
  112.  
  113.     public static int FindSize(GameViewSizeGroupType sizeGroupType, string text) {
  114.         // GameViewSizes group = gameViewSizesInstance.GetGroup(sizeGroupType);
  115.         // string[] texts = group.GetDisplayTexts();
  116.         // for loop...
  117.  
  118.         var group = GetGroup(sizeGroupType);
  119.         var getDisplayTexts = group.GetType().GetMethod("GetDisplayTexts");
  120.         var displayTexts = getDisplayTexts.Invoke(group, null) as string[];
  121.         for (int i = 0; i < displayTexts.Length; i++) {
  122.             string display = displayTexts[i];
  123.             // the text we get is "Name (W:H)" if the size has a name, or just "W:H" e.g. 16:9
  124.             // so if we're querying a custom size text we substring to only get the name
  125.             // You could see the outputs by just logging
  126.             // Debug.Log(display);
  127.             int pren = display.IndexOf('(');
  128.             if (pren != -1)
  129.                 display = display.Substring(0, pren - 1); // -1 to remove the space that's before the prens. This is very implementation-depdenent
  130.             if (display == text)
  131.                 return i;
  132.         }
  133.         return -1;
  134.     }
  135.     static object GetGroup(GameViewSizeGroupType type) {
  136.         return getGroup.Invoke(gameViewSizesInstance, new object[] { (int)type });
  137.     }
  138. }
Add Comment
Please, Sign In to add comment