Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Maximize editor game view in code

Discussion in 'Scripting' started by eneroth3, Dec 21, 2018.

  1. eneroth3

    eneroth3

    Joined:
    Oct 22, 2018
    Posts:
    63
    I've been working on a small script to toggle fullscreen mode in a standalone game. This is by default triggered by F11, but I plan to make it user configurable, as well as adding a toggle button to the graphics menu.

    However, I also switch a lot between maximized and not maximized mode in the editor when developing the game. Is it possible to hook this up to my script, so I can use the same shortcut and menu entry as I use for the standalone game?

    I'm already using a similar approach for the quit button in my pause menu.

    Code (csharp):
    1.  
    2. public void Quit()
    3.     {
    4.         #if UNITY_EDITOR
    5.         UnityEditor.EditorApplication.isPlaying = false;
    6.         #else
    7.         Application.Quit();
    8.         #endif
    9.     }
    10.  
    The problem I have now is that I can't find any API hooks for toggling maximized game view in the editor. Is there one?
     
    Eristen and Hunter_Bobeck like this.
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,589
    Here is what I came up with:
    Code (CSharp):
    1.     public static void SetResolution(int width, int height, bool fullscreen, int preferredRefreshRate = 0)
    2.     {
    3.         Screen.SetResolution(width, height, fullscreen, preferredRefreshRate);
    4.      
    5. #if UNITY_EDITOR
    6.         var windows = (UnityEditor.EditorWindow[])Resources.FindObjectsOfTypeAll(typeof(UnityEditor.EditorWindow));
    7.         foreach(var window in windows)
    8.         {
    9.             if(window != null && window.GetType().FullName == "UnityEditor.GameView")
    10.             {
    11.                 window.maximized = fullscreen;
    12.                 break;
    13.             }
    14.         }
    15.  
    16.         UnityEditorInternal.InternalEditorUtility.RepaintAllViews();
    17. #endif
    18.     }
    The idea is that I search for the GameView EditorWindow and then just set its maximized flag.
     
    dvalles and crowejohn20 like this.
  3. eneroth3

    eneroth3

    Joined:
    Oct 22, 2018
    Posts:
    63
    Thanks!

    Being new to editor scripting I didn't know of the EditorWindow class.

    I've got a toggle function to work with only 2 lines of code in the body.
    Code (csharp):
    1.  
    2. private void ToggleInEditor()
    3. {
    4.     EditorWindow window = EditorWindow.focusedWindow;
    5.     // Assume the game view is focused.
    6.     window.maximized = !window.maximized;
    7. }
    8.  
    As long as the function is called from direct user input, not a timer, I think the game view will be focused.
     
    Eristen and crowejohn20 like this.
  4. eneroth3

    eneroth3

    Joined:
    Oct 22, 2018
    Posts:
    63
    Follow up question, is there a way to access the Maximize on Play button state from a script? It would be nice to also set this value in my toggle function, to make the state persistent between game runs, and be consistent to how standalone games are handled.

    Unfortunately it's hard to Google on the subject, as all results seem to be about people wanting full full screen in the editor.
     
  5. eneroth3

    eneroth3

    Joined:
    Oct 22, 2018
    Posts:
    63
    If anyone else finds it useful, here is my fullscreen toggle class.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityStandardAssets.CrossPlatformInput;
    4. #if UNITY_EDITOR
    5. using UnityEditor;
    6. #endif
    7.  
    8. public class FullscreenToggle : MonoBehaviour {
    9.    private void Update () {
    10.         // REVIEW: Is CrossPlatform amanager better than native Input, or not?
    11.         // https://forum.unity.com/threads/what-is-crossplatforminputmanager-for.601594/#post-4022713
    12.         if (CrossPlatformInputManager.GetButtonDown("FullScreen"))
    13.             Toggle();
    14.     }
    15.  
    16. #if UNITY_EDITOR
    17.  
    18.     public bool IsFullScreen()
    19.     {
    20.         // Assume the game view is focused.
    21.         return EditorWindow.focusedWindow.maximized;
    22.     }
    23.  
    24.     public void Toggle()
    25.     {
    26.         EditorWindow window = EditorWindow.focusedWindow;
    27.         // Assume the game view is focused.
    28.         window.maximized = !window.maximized;
    29.     }
    30.  
    31. #else
    32.  
    33.     public bool IsFullScreen()
    34.     {
    35.         return Screen.fullScreen;
    36.     }
    37.  
    38.     public void Toggle()
    39.     {
    40.         // As of now, unity doesn't handle resolution and full screen toggling
    41.         // well. When merely enetering full screen, the old resolution is kept,
    42.         // causing a pixelated view with a faulty aspect ratio. On Windows
    43.         // black bars are displayed and on Mac the view is stretched.
    44.         // Instead of below line of code, use custom functions that also
    45.         // updates the resolution.
    46.         //Screen.fullScreen = !Screen.fullScreen;
    47.  
    48.         if (IsFullScreen())
    49.             RestoreWindow();
    50.         else
    51.             EnterFullScreen();
    52.     }
    53.  
    54.     private void RestoreWindow()
    55.     {
    56.         // TODO: If possible, also set window position.
    57.         int width = PlayerPrefs.GetInt("WindowWidth", 800);
    58.         int height = PlayerPrefs.GetInt("WindowHeight", 600);
    59.         Screen.SetResolution(width, height, false);
    60.     }
    61.  
    62.     private void EnterFullScreen()
    63.     {
    64.         PlayerPrefs.SetInt("WindowWidth", Screen.width);
    65.         PlayerPrefs.SetInt("WindowHeight", Screen.height);
    66.  
    67.         // REVIEW: Can it safely be assumed the game runs in the main monitor?
    68.         Screen.SetResolution(Display.main.systemWidth, Display.main.systemHeight, true);
    69.     }
    70.  
    71. #endif
    72. }
    73.  
    74.  
     
    mklasson likes this.
  6. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,589
    Look at the GameView source code:
    https://github.com/Unity-Technologies/UnityCsReference/blob/master/Editor/Mono/GameView/GameView.cs

    You should find:
    Code (CSharp):
    1. internal class GameView : EditorWindow
    2. {
    3.     [SerializeField] bool m_MaximizeOnPlay;
    4.  
    5.     public bool maximizeOnPlay
    6.     {
    7.         get { return m_MaximizeOnPlay; }
    8.         set { m_MaximizeOnPlay = value; }
    9.     }
    Since the GameView class has internal visibility only, you can't cast the EditorWindow to GameView. However, I can think of two ways to get the maximizeOnPlay value:
     
  7. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,589
  8. eneroth3

    eneroth3

    Joined:
    Oct 22, 2018
    Posts:
    63
    I've considered using Window's specific API for this, but it wouldn't work on other platforms. On the other hand, the game doesn't break from this being Windows only, and a large percentage of players will be on Windows anyway.

    Regarding the maximize on play toggle it blew my mind that the source code for these classes are publicly available! While it's a hack that may break in newer versions of the editor to rely on internal functionality, it's definitely something interesting to look into!
     
    Peter77 likes this.
  9. dvalles

    dvalles

    Joined:
    Nov 15, 2014
    Posts:
    11
    Epic script. Thanks.
     
    Peter77 likes this.