Search Unity

Fullscreening a fixed resolution build forever changes the default resolution of that build

Discussion in 'Windows' started by kahshmick, May 22, 2023.

  1. kahshmick

    kahshmick

    Joined:
    Dec 27, 2016
    Posts:
    7
    Hello, I'm running into something interesting with my build. When I fullscreen a build it works correctly, but when pressing the fullscreen button again to return to windowed the window is now the size of the monitor it was fullscreened on! Not only that, but the build's resolution is now locked to this forever, even when sleeping or restarting the computer.

    What I imagine to be correct behavior
    - Fullscreen button takes windowed app and makes it fullscreen
    - Fullscreen button again returns window to it's earlier size/position
    Most apps have this behavior I think, for instance windows Powershell

    In the player settings I have
    - Fullscreen Mode: Windowed
    - Default Screen Width/Height: 1920x1080
    - Resizable Window = false
    - Allow Fullscreen Switch = true

    I can imagine creating a script to save the resolution on fullscreen and restore it manually when fullscreen ends, but this problems seems like it should have a better solution. Has anyone encountered this?

    Because this is multi platform I should clarify I'm encountering this on Windows, and using the fullscreen shortcut (alt+enter) to test here.
     
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    This is unfortunately how it works. "Remembering" windowed mode resolution is not implemented in Unity on Windows.
     
  3. kahshmick

    kahshmick

    Joined:
    Dec 27, 2016
    Posts:
    7
    I see, thank you for the quick explanation! Here's the solution I ended up with that works relatively well. This script spawns a singleton that ensures leaving fullscreen will set the application to a specific resolution.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. /// <summary>
    7. /// Makes sure that the resolution returns to default when exiting fullscreen.
    8. /// Otherwise when exiting fullscreen the windowed resolution will be equal to the monitor's fullscreen resolution.
    9. /// </summary>
    10. public class ManageFullscreenSwitch : MonoBehaviour {
    11.     private static ManageFullscreenSwitch instance;
    12.  
    13.     // This will override the default resolution settings, proceed with caution!
    14.     // These two values control in the entire project, overriding the player settings
    15.     private int _defaultWidth = 1920;
    16.     private int _defaultHeight = 1080;
    17.     private bool _fullscreen = false;
    18.  
    19.     [RuntimeInitializeOnLoadMethod]
    20.     static void InitFullscreenSingleton() {
    21.         if (instance) return;
    22.         GameObject g = new GameObject();
    23.         g.name = "Singleton_ManageFullScreenSwitch";
    24.         instance = g.AddComponent<ManageFullscreenSwitch>();
    25.         Debug.Log("Created ManageFullscreenSwitch singleton.");
    26.         DontDestroyOnLoad(g);
    27.     }
    28.  
    29.     private void Update() {
    30.         if (_fullscreen != Screen.fullScreen) {
    31.             if (!Screen.fullScreen) {
    32.                 Screen.SetResolution(_defaultWidth, _defaultHeight, false);
    33.             }
    34.  
    35.             _fullscreen = Screen.fullScreen;
    36.         }
    37.     }
    38. }
    This works okay, but there are still some things that are irking me:
    - When the player is closed in fullscreen, it will start in fullscreen too. I would like the app to always start windowed at the default resolution. I tried setting the resolution when the app closes to circumvent this, but it doesn't seem to work. I can also set the resolution manually at startup, but I can't do this before the splash screen plays using the wrong fullscreen mode. This seems difficult to control.


    - This script completely overrides the player settings for default resolution, which will be really confusing for anyone working on my project in the future. Is there any way to read the default with/height at runtime instead of using script driven values?
     
  4. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    You could build with "Generate Visual Studio solution" option once and change the entry point to always pass "-screen-fullscreen 0" command line arguments to the `UnityMain` function. Once you recompile the executable once, you can copy it into your project and use a postprocess script to overwrite the one Unity produces in a regular build.

    Similarly to the above, you could write a postprocess script to read the default width/height from player settings a build time, write that to a file in the data folder and then modify the executable to read that file on startup and pass those values to UnityMain using the "-screen-width" and "-screen-height" command line arguments.

    If you don't mind the window jumping on startup, you could do that all in script. But it would look nicer if you overrode it via startup arguments.
     
  5. kahshmick

    kahshmick

    Joined:
    Dec 27, 2016
    Posts:
    7
    Ahh I see, maybe I'll get to that point but for now I'm starting to see this as expected UI behavior. Thank you for the thorough responses! I would prefer to avoid building direct from visual studio, so perhaps for the second issue I'll write the playersettings values to a prefab during the build