Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEditor;
- using UnityEngine;
- using System.Collections;
- using System.Reflection;
- using System;
- /// <summary>
- /// From https://www.reddit.com/r/Unity3D/comments/2lymim/full_full_screen_on_play_script_freebie_for/
- /// and https://answers.unity.com/questions/956123/add-and-select-game-view-resolution.html
- /// </summary>
- [InitializeOnLoad]
- public class FullscreenPlayMode : MonoBehaviour {
- public static bool fullscreenEnabled = true;
- //The size of the toolbar above the game view, excluding the OS border.
- private static int tabHeight = 22;
- static object gameViewSizesInstance;
- static MethodInfo getGroup;
- static FullscreenPlayMode() {
- if (!fullscreenEnabled) return;
- EditorApplication.playModeStateChanged -= CheckPlayModeState;
- EditorApplication.playModeStateChanged += CheckPlayModeState;
- var sizesType = typeof(Editor).Assembly.GetType("UnityEditor.GameViewSizes");
- var singleType = typeof(ScriptableSingleton<>).MakeGenericType(sizesType);
- var instanceProp = singleType.GetProperty("instance");
- getGroup = sizesType.GetMethod("GetGroup");
- gameViewSizesInstance = instanceProp.GetValue(null, null);
- }
- /// <summary>
- /// Only called with EnteredPlayMode and EnteredEditMode, never Exit for some reason in 2017.3.0f3
- /// </summary>
- static void CheckPlayModeState(PlayModeStateChange state) {
- if (!fullscreenEnabled) return;
- if (state == PlayModeStateChange.EnteredPlayMode) {
- FullScreenGameWindow();
- } else if (state == PlayModeStateChange.EnteredEditMode) {
- CloseGameWindow();
- }
- }
- static EditorWindow GetMainGameView() {
- EditorApplication.ExecuteMenuItem("Window/Game");
- 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;
- }
- static void FullScreenGameWindow() {
- EditorWindow gameView = GetMainGameView();
- //gameView.title = "Game (Stereo)";
- Rect newPos = new Rect(0, 0 - tabHeight, Screen.currentResolution.width, Screen.currentResolution.height + tabHeight);
- // move down to second monitor beneath main one
- newPos.y += 1080;
- gameView.position = newPos;
- gameView.minSize = new Vector2(Screen.currentResolution.width, Screen.currentResolution.height + tabHeight);
- gameView.maxSize = gameView.minSize;
- gameView.position = newPos;
- // since camera may have moved, re-align the scene view to it
- NWUtils.RunLater(NWUtils.AlignSceneToCamera, 0.5f, true);
- }
- static void CloseGameWindow() {
- EditorWindow gameView = GetMainGameView();
- gameView.Close();
- Type gameViewType = Type.GetType("UnityEditor.GameView,UnityEditor.dll");
- Type[] desiredDockNextTo = new Type[] { typeof(UnityEditor.SceneView) };
- MethodInfo getWindow = typeof(EditorWindow).GetMethod("GetWindow", new Type[] { typeof(string), typeof(bool), desiredDockNextTo.GetType() });
- MethodInfo getWindowGameView = getWindow.MakeGenericMethod(gameViewType);
- getWindowGameView.Invoke(null, new object[] { "Game", false, desiredDockNextTo });
- // set the game screen size back to 16:9
- SetWidescreen();
- // set focus back on Scene tab
- EditorWindow.FocusWindowIfItsOpen(typeof(UnityEditor.SceneView));
- // since camera may have moved, re-align the scene view to it
- NWUtils.AlignSceneToCamera();
- }
- public static void MaximizeGameWindow() {
- var assembly = Assembly.GetAssembly(typeof(Editor));
- var type = assembly.GetType("UnityEditor.GameView");
- var property = type.GetProperty("maximized");
- var window = EditorWindow.GetWindow(type);
- var maximized = (bool)property.GetValue(window, null);
- property.SetValue(window, (object)(!maximized), null);
- }
- public static void SetWidescreen() {
- SetSize(FindSize(GameViewSizeGroupType.Standalone, "16:9"));
- }
- public static void SetSize(int index) {
- var gvWndType = typeof(Editor).Assembly.GetType("UnityEditor.GameView");
- var selectedSizeIndexProp = gvWndType.GetProperty("selectedSizeIndex",
- BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
- var gvWnd = EditorWindow.GetWindow(gvWndType);
- selectedSizeIndexProp.SetValue(gvWnd, index, null);
- }
- public static int FindSize(GameViewSizeGroupType sizeGroupType, string text) {
- // GameViewSizes group = gameViewSizesInstance.GetGroup(sizeGroupType);
- // string[] texts = group.GetDisplayTexts();
- // for loop...
- var group = GetGroup(sizeGroupType);
- var getDisplayTexts = group.GetType().GetMethod("GetDisplayTexts");
- var displayTexts = getDisplayTexts.Invoke(group, null) as string[];
- for (int i = 0; i < displayTexts.Length; i++) {
- string display = displayTexts[i];
- // the text we get is "Name (W:H)" if the size has a name, or just "W:H" e.g. 16:9
- // so if we're querying a custom size text we substring to only get the name
- // You could see the outputs by just logging
- // Debug.Log(display);
- int pren = display.IndexOf('(');
- if (pren != -1)
- display = display.Substring(0, pren - 1); // -1 to remove the space that's before the prens. This is very implementation-depdenent
- if (display == text)
- return i;
- }
- return -1;
- }
- static object GetGroup(GameViewSizeGroupType type) {
- return getGroup.Invoke(gameViewSizesInstance, new object[] { (int)type });
- }
- }
Add Comment
Please, Sign In to add comment