Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Fullscreen-Windowed Mode Not Switching

Discussion in 'Scripting' started by Game_Genesis, Jan 10, 2021.

  1. Game_Genesis

    Game_Genesis

    Joined:
    Mar 23, 2019
    Posts:
    3
    (2020.2.0f1)
    Hello,
    I've been struggling with this issue for about two days now. I have a toggle to change between fullscreen and windowed mode, and it just doesn't work.

    If I set Fullscreen Mode in the Player Settings to Windowed, the game stays in windowed mode even when Screen.fullscreen is set to true. If I set Fullscreen Mode to anything else (Exclusive Fullscreen, Fullscreen Window, Maximized Window), the game remains in exclusive fullscreen.

    I've tried setting the FullscreenMode in code, and it doesn't change anything.

    I've tried a variety of things and it still doesn't work:
    Code (CSharp):
    1.     public void SetFullscreen(bool fullscreen)
    2.     {
    3.         Screen.fullScreen = fullscreen;
    4.     }
    And this:
    Code (CSharp):
    1.     public void SetFullscreen(bool fullscreen)
    2.     {
    3.         Screen.fullScreen = fullscreen;
    4.         Screen.SetResolution(Screen.width, Screen.height, true);
    5.         if (Screen.fullScreen)
    6.         {
    7.             Screen.fullScreenMode = FullScreenMode.ExclusiveFullScreen;
    8.         }
    9.         else
    10.         {
    11.             Screen.fullScreenMode = FullScreenMode.Windowed;
    12.         }
    13.     }
    I've also tried:
    Code (CSharp):
    1.     public void SetFullscreenOverride(bool fullscreen)
    2.     {
    3.         Screen.fullScreen = fullscreen;
    4.         if (fullscreen)
    5.         {
    6.             Screen.SetResolution(Screen.width, Screen.height, FullScreenMode.ExclusiveFullScreen);
    7.         }
    8.         else
    9.         {
    10.             Screen.SetResolution(Screen.width, Screen.height, FullScreenMode.Windowed);
    11.         }
    12.     }
    And nothing works.

    Only Alt+Enter switches between the two modes.

    I've tried all the resolution settings. I've done resizable window, Capture Single Screen, etc.

    Player Settings.PNG

    Thank you
     
    Threeyes likes this.
  2. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    685
    I've had numerous problems with this. There are (I believe) some bugs, though it's tough to pinpoint. Drove me nuts...
     
  3. Game_Genesis

    Game_Genesis

    Joined:
    Mar 23, 2019
    Posts:
    3
    I still haven't come up with a solution, even after searching every forum. This is driving me crazy
     
  4. ksc_3899

    ksc_3899

    Joined:
    Jan 1, 2019
    Posts:
    30
    Any solution on this please? I am stuck here :((
     
  5. turbolek

    turbolek

    Joined:
    Dec 30, 2016
    Posts:
    10
    Same here
     
  6. marspot

    marspot

    Joined:
    May 22, 2020
    Posts:
    3
    I'm also having issues with this. Any solutions yet?
     
  7. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    1,112
    Here is the class I use in my Settings Generator Asset. It has worked for many users so far and covers a wide range of Unity versions.

    The key takeaway might be this: All the screen related functions are async (executed at the end of the frame) and some are contradicting each other, for example: Screen.fullScreen = true VS Screen.fullScreenMode = FullScreenMode.Windowed.

    Usage:
    Code (csharp):
    1. /// <summary>
    2. /// NOTICE: A full screen switch does not happen immediately; it happens when the current frame is finished.
    3. /// See: https://docs.unity3d.com/ScriptReference/Screen-fullScreen.html
    4. /// </summary>
    5. /// <param name="fullScreen">Fullscreen on or off.</param>
    6. public void Set(bool fullScreen)
    7. {
    8.    // Request change but delegate the actual execution to the orchestrator.
    9.    ScreenOrchestrator.Instance.RequestFullScreen(fullScreen);
    10. }
    The class:
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3.  
    4. namespace Kamgam.SettingsGenerator
    5. {
    6.     /// <summary>
    7.     /// All the screen related functions are async (executed at the end of the frame)<br />
    8.     /// and some are contradicting each other, for example:
    9.     /// Screen.fullScreen = true VS Screen.fullScreenMode = FullScreenMode.Windowed
    10.     ///
    11.     /// To resove this it was decided that Screen.fullScreenMode always takes precedence.
    12.     ///
    13.     /// There fore we need this helper to execute them properly in order.
    14.     /// </summary>
    15.     public class ScreenOrchestrator : MonoBehaviour
    16.     {
    17.         private static ScreenOrchestrator _instance;
    18.         public static ScreenOrchestrator Instance
    19.         {
    20.             get
    21.             {
    22.                 if (!_instance)
    23.                 {
    24. #if UNITY_EDITOR
    25.                     // Keep the instance null outside of play mode to avoid leaking
    26.                     // instances into the scene.
    27.                     if (!UnityEditor.EditorApplication.isPlaying)
    28.                     {
    29.                         return null;
    30.                     }
    31. #endif
    32.                     _instance = new GameObject().AddComponent<ScreenOrchestrator>();
    33.                     _instance.name = _instance.GetType().ToString();
    34. #if UNITY_EDITOR
    35.                     _instance.hideFlags = HideFlags.DontSave;
    36.                     if (UnityEditor.EditorApplication.isPlaying)
    37.                     {
    38. #endif
    39.                         DontDestroyOnLoad(_instance.gameObject);
    40. #if UNITY_EDITOR
    41.                     }
    42. #endif
    43.                 }
    44.                 return _instance;
    45.             }
    46.         }
    47.  
    48.         protected Resolution? requestedResolution;
    49. // Notice: The RefreshRate class has been added in 2021_2 (yes 2021) BUT it is used in Screen.SetResolution only in 2022.2 (yes 2022, not 2021)
    50. #if UNITY_2022_2_OR_NEWER
    51.         protected RefreshRate? requestedRefreshRate;
    52. #else
    53.         protected int? requestedRefreshRate;
    54. #endif
    55.  
    56.         protected bool? requestedFullScreen;
    57.         protected FullScreenMode? requestedFullScreenMode;
    58.  
    59.         protected Coroutine _applyCoroutine;
    60.  
    61.         public void RequestResolution(Resolution resolution)
    62.         {
    63.             requestedResolution = resolution;
    64.         }
    65.  
    66. // Notice: The RefreshRate class has been added in 2021_2 (yes 2021) BUT it is used in Screen.SetResolution only in 2022.2 (yes 2022, not 2021)
    67. #if UNITY_2022_2_OR_NEWER
    68.         public void RequestRefreshRate(RefreshRate refreshRate)
    69.         {
    70.             requestedRefreshRate = refreshRate;
    71.         }
    72. #else
    73.         public void RequestRefreshRate(int refreshRate)
    74.         {
    75.             requestedRefreshRate = refreshRate;
    76.         }
    77. #endif
    78.  
    79.         public void RequestFullScreen(bool fullScreen)
    80.         {
    81.             requestedFullScreen = fullScreen;
    82.         }
    83.  
    84.         public void RequestFullScreenMode(FullScreenMode fullScreenMode)
    85.         {
    86.             requestedFullScreenMode = fullScreenMode;
    87.         }
    88.  
    89.         public void LateUpdate()
    90.         {
    91.             if (requestedResolution.HasValue || requestedFullScreen.HasValue || requestedFullScreenMode.HasValue)
    92.             {
    93.                 apply();
    94.             }
    95.         }
    96.  
    97.         protected void apply()
    98.         {
    99.             if (_applyCoroutine != null)
    100.             {
    101.                 StopCoroutine(_applyCoroutine);
    102.             }
    103.  
    104.             _applyCoroutine = StartCoroutine(applyStaggered());
    105.         }
    106.  
    107.         protected IEnumerator applyStaggered()
    108.         {
    109.             // Copy
    110.             var tRequestedFullScreen = requestedFullScreen;
    111.             var tRequestedFullScreenMode = requestedFullScreenMode;
    112.             var tRequestedResolution = requestedResolution;
    113.             var tRequestedRefreshRate = requestedRefreshRate;
    114.  
    115.             // Reset immediately
    116.             requestedFullScreen = null;
    117.             requestedFullScreenMode = null;
    118.             requestedResolution = null;
    119.             requestedRefreshRate = null;
    120.  
    121.             if (tRequestedFullScreen.HasValue)
    122.             {
    123.                 if (!tRequestedFullScreen.Value)
    124.                 {
    125.                     Screen.fullScreen = false;
    126.                 }
    127.                 else
    128.                 {
    129.                     Screen.fullScreen = true;
    130.                 }
    131.  
    132.                 // Wait one frame
    133.                 yield return null;
    134.             }
    135.  
    136.             if (tRequestedFullScreenMode.HasValue)
    137.             {
    138.                 Screen.fullScreenMode = tRequestedFullScreenMode.Value;
    139.  
    140.                 // Wait one frame
    141.                 yield return null;
    142.             }
    143.  
    144.             if (tRequestedResolution.HasValue)
    145.             {
    146.                 var resolution = tRequestedResolution.Value;
    147. // Notice: The RefreshRate class has been added in 2021_2 (yes 2021) BUT it is used in Screen.SetResolution only in 2022.2 (2022, not 2021)
    148. #if UNITY_2022_2_OR_NEWER
    149.                 var refreshRate = tRequestedRefreshRate.HasValue ? tRequestedRefreshRate.Value : tRequestedResolution.Value.refreshRateRatio;
    150. #else
    151.                 var refreshRate = tRequestedRefreshRate.HasValue ? tRequestedRefreshRate.Value : tRequestedResolution.Value.refreshRate;
    152. #endif
    153.                 var fullScreenMode = tRequestedFullScreenMode.HasValue ? tRequestedFullScreenMode.Value : Screen.fullScreenMode;
    154.                 Screen.SetResolution(resolution.width, resolution.height, fullScreenMode, refreshRate);
    155.             }
    156.             else if (tRequestedRefreshRate.HasValue)
    157.             {
    158.                 var res = Screen.currentResolution;
    159.                 var fullScreenMode = tRequestedFullScreenMode.HasValue ? tRequestedFullScreenMode.Value : Screen.fullScreenMode;
    160.                 Screen.SetResolution(res.width, res.height, fullScreenMode, tRequestedRefreshRate.Value);
    161.             }
    162.         }
    163.  
    164.         public void Destroy()
    165.         {
    166.             _instance = null;
    167.  
    168.             if (this != null && this.gameObject != null)
    169.             {
    170. #if UNITY_EDITOR
    171.                 if (UnityEditor.EditorApplication.isPlaying)
    172.                 {
    173.                     Destroy(this.gameObject);
    174.                 }
    175.                 else
    176.                 {
    177.                     DestroyImmediate(this.gameObject);
    178.                 }
    179. #else
    180.                 Destroy(this.gameObject);
    181. #endif
    182.             }
    183.         }
    184.     }
    185. }
    186.  
     
    Last edited: Jul 8, 2023
    Ryiah likes this.
  8. Game_Genesis

    Game_Genesis

    Joined:
    Mar 23, 2019
    Posts:
    3
    It's been a bit over two years now, but I'm pretty sure updating Unity finally resolved the issue for me