Search Unity

Bug Build refuses to switch between fullscreen/windowed mode

Discussion in 'Editor & General Support' started by HomeBearJosh, Feb 15, 2023.

  1. HomeBearJosh

    HomeBearJosh

    Joined:
    Dec 13, 2017
    Posts:
    33
    Code that I've used in 2018.1.6f1 for switching from fullscreen to windowed mode is not working for me in 2020.2.2f1. In my build, the game simply refuses to switch between fullscreen/windowed.
    Alt-tabbing seems to causes freezes; there's that, too.

    I've set player settings specifically to start the game in Windowed mode, for testing purposes.
    However, the game still starts in fullscreen mode. I've deleted all saved data; serialized data shouldn't affect this.
    player_settings.jpg

    The code is pretty simple; fullscreen mode is set to a toggle which value is used in Screen.SetResolution().
    The first single line used to work just fine; I added the extra bit to... make really sure.
    Code (CSharp):
    1.        
    2. Screen.SetResolution(model.resolutionWidth, model.resolutionHeight, model.fullscreenToggle.isOn);
    3.         if (!model.fullscreenToggle.isOn)
    4.         {
    5.             Screen.fullScreen = false;
    6.             Screen.fullScreenMode = FullScreenMode.Windowed;
    7.         }
    8.         App.LogWarning("Screen mode has been set to: " + Screen.fullScreenMode);
    9.  
    Even after explicitly setting Screen.fullScreenMode to Windowed, Player.log still states:
    "Screen mode has been set to: FullScreenWindow".

    The toggle itself works just fine, and prints correct 'true' or 'false' values depending on its state.
    What am I missing here?
     

    Attached Files:

  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,675
    Calling Screen.SetResolution and setting Screen.fullScreen/Screen.fullScreenMode merely enqueues the resolution change request. It then gets executed after the current frame finishes. So if you're querying the status immediately, you will see the same value. Try logging it the next frame.
     
  3. HomeBearJosh

    HomeBearJosh

    Joined:
    Dec 13, 2017
    Posts:
    33
    I should have mentioned that - changing resolutions does actually work (from 1080p to 720p, for instance), it's just that the game stays fullscreen no matter what. If I understand what you're saying correctly, shouldn't that also avoid the resolution from being changed?

    If I'm missing the point, could you oblige me and add a short code example?
     
  4. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,675
    Code (csharp):
    1. IEnumerator SwitchToWindowed()
    2. {
    3.     Screen.SetResolution(model.resolutionWidth, model.resolutionHeight, FullScreenMode.Windowed);
    4.     yield null;
    5.     App.LogWarning("Screen mode has been set to: " + Screen.fullScreenMode);
    6. }
    7.  
    8. void Start()
    9. {
    10.     StartCoroutine(SwitchToWindowed());
    11. }
    If that doesn't work, something weird is going on. Next thing to try would be doing such a script in an empty project. Perhaps something in your project is forcing fullscreen? Do you have any errors in the player log?
     
    HomeBearJosh likes this.
  5. HomeBearJosh

    HomeBearJosh

    Joined:
    Dec 13, 2017
    Posts:
    33
    You absolutely nailed it.
    Thanks for your help!