Search Unity

Bug Resolution changes when setting to windowed mode at max resolution accepted by monitor

Discussion in 'General Graphics' started by ObsidianSpire, May 1, 2023.

  1. ObsidianSpire

    ObsidianSpire

    Joined:
    Sep 13, 2013
    Posts:
    48
    Hi, so I've been working on the menu for a game and I'm running into an issue where going windowed at the max resolution supported by the monitor leaves it at a different resolution, but my attempts to detect the lowered resolution don't seem to work. In the video, my friend demonstrates going from 1920 x 1080 FullScreen Windowed to Windowed, which sets the actual size of the window to 1600 x 900.



    I've attached the resolution/fullscreen portions of the script handling the menu. What's interesting is on my machine, which is a laptop, the code leaves the windowed version at full resolution but partially off-screen since it is too big, so my code works at least some of the time.

    If someone has an idea on how to fix this, please let me know, it's been bothering me for weeks.

    Unity personal version: 2020.3.37f1
    We plan to upgrade to the last LTS release of 2020 when that comes, but we've stuck with other versions of 2020 for a while.

    Code (CSharp):
    1.  
    2.  
    3. // Start is called before the first frame update
    4.     void Start()
    5.     {
    6.         if( !shouldIInit )
    7.         {
    8.             CloseAllMenus(closeAllMenusOnStart);
    9.             rePlayer = ReInput.players.GetPlayer(playerId);
    10.             gs = GameStates.Active;
    11.             playerObj = GameObject.FindGameObjectWithTag("Player");
    12.             if (playerObj != null)
    13.             {
    14.                 pcontroller = playerObj.GetComponent<PlayerController_MainCharacter>();
    15.             }
    16.             //Fetch the current EventSystem. Make sure your Scene has one.
    17.             m_EventSystem = EventSystem.current;
    18.             lm = GameObject.FindObjectOfType<LoadManager>();
    19.             return;
    20.         }
    21.  
    22.         lm = GameObject.FindObjectOfType<LoadManager>();
    23.         //Fetch the current EventSystem. Make sure your Scene has one.
    24.         m_EventSystem = EventSystem.current;
    25.  
    26.         if (PlayerPrefs.GetInt("UnitySelectMonitor", 0) == -1)
    27.         {
    28.             PlayerPrefs.SetInt("UnitySelectMonitor", 0);
    29.             //monDropdown.value = 0;
    30.             //SetMonitor();
    31.         }
    32.         else
    33.         {
    34.             //monDropdown.value = PlayerPrefs.GetInt("UnitySelectMonitor", 0);
    35.             //SetMonitor();
    36.         }
    37.  
    38.         [B]resolutions = Screen.resolutions;
    39.         //Debug.LogError(resolutions.Length);
    40.         //resDropdown.ClearOptions();
    41.         resDropdownTMPro.ClearOptions();
    42.  
    43.         List<string> options = new List<string>();
    44.         int currentResolutionIndex = 0;
    45.         for(int i = 1; i < resolutions.Length; i++)
    46.         {
    47.             string option = resolutions[i].width + " x " + resolutions[i].height;
    48.             if (!options.Contains(option))
    49.             {
    50.                 options.Add(option);
    51.             }
    52.  
    53.             if(resolutions[i].width == Screen.width && resolutions[i].height == Screen.height)
    54.             {
    55.                 currentResolutionIndex = i;
    56.                 //Debug.LogError(i);
    57.             }
    58.         }
    59.  
    60.         int curRes = PlayerPrefs.GetInt("CurrentResolution", resolutions.Length - 2);
    61.  
    62.         /*
    63.         resDropdown.AddOptions(options);
    64.         resDropdown.value = curRes;
    65.         //resDropdown.value = currentResolutionIndex;
    66.         resDropdown.RefreshShownValue();
    67.         */
    68.  
    69.         resDropdownTMPro.AddOptions(options);
    70.         resDropdownTMPro.value = curRes;
    71.         resDropdownTMPro.RefreshShownValue();[/B]
    72.  
    73.  
    74.  
    75.       [B]  int selectedFullscreenMode = PlayerPrefs.GetInt("SelectedFullscreenMode", 0);
    76.         //fsDropdown.value = selectedFullscreenMode;
    77.         fsDropdownTMPro.value = selectedFullscreenMode;
    78.  
    79.         SetResolution();[/B]
    80.  
    81.         // Make the game run as fast as possible
    82.         Application.targetFrameRate = 300;
    83.         int savedVsync = PlayerPrefs.GetInt("EnableVsync", 1);
    84.         if(savedVsync == 1)
    85.         {
    86.             vsyncTog.isOn = true;
    87.          
    88.         }
    89.         else
    90.         {
    91.             vsyncTog.isOn = false;
    92.         }
    93.         // Turn on/off v-sync
    94.         SetVsyncCount();
    95.         Debug.Log(Screen.currentResolution.refreshRate + " : refresh rate of monitor!");
    96.         //QualitySettings.vSyncCount = 0;
    97.  
    98.         //Cursor.lockState = CursorLockMode.Locked;
    99.         rePlayer = ReInput.players.GetPlayer(playerId);
    100.      
    101.         gs = GameStates.Active;
    102.  
    103.         //Debug.Log("displays connected: " + Display.displays.Length);
    104.         // Display.displays[0] is the primary, default display and is always ON, so start at index 1.
    105.         // Check if additional displays are available and activate each.
    106.  
    107.         //options.Clear();
    108.         //for (int i = 0; i < Display.displays.Length; i++)
    109.         //{
    110.         //    //if (i > 0)
    111.         //        //Display.displays[i].Activate();
    112.         //    string option = ("Monitor " + (i + 1));
    113.         //    if (!options.Contains(option))
    114.         //        options.Add(option);
    115.  
    116.         //}
    117.         //monDropdown.AddOptions(options);
    118.         //monDropdown.value = PlayerPrefs.GetInt("UnitySelectMonitor");
    119.         //monDropdown.RefreshShownValue();
    120.         //SetMonitor();
    121.  
    122.         //load saved sfx/music volume
    123.         float storedMusic = Mathf.Clamp(PlayerPrefs.GetFloat("StoredMusicVolume", 0.5f), 0.0f, 1.0f);
    124.         musicSlider.value = storedMusic;
    125.         SetMusicVolume();
    126.         float storedSFX = Mathf.Clamp(PlayerPrefs.GetFloat("StoredSFXVolume", 0.5f), 0.0f, 1.0f);
    127.         sfxSlider.value = storedSFX;
    128.         SetSFXVolume();
    129.  
    130.         if (myControlMapper == null)
    131.         {
    132.             myControlMapper = GameObject.FindGameObjectWithTag("RewiredControlMapper").GetComponent<ControlMapper>();
    133.         }
    134.  
    135.         /*
    136.         string deviceID = SystemInfo.deviceModel;
    137.         if (deviceID == "iPhone10,3 " || deviceID == "iPhone10,6")
    138.         {
    139.             // This is an iPhoneX
    140.             //now change the graphics tier to tier 3
    141.             Graphics.activeTier = GraphicsTier.Tier3;
    142.         }
    143.         */
    144.  
    145.         if(Graphics.activeTier != GraphicsTier.Tier3)
    146.         {
    147.             Graphics.activeTier = GraphicsTier.Tier3;
    148.         }
    149.         CloseAllMenus(true);
    150.         if (parentMainMenuCanvas != null)
    151.         {
    152.             parentMainMenuCanvas.SetActive(true);
    153.         }
    154.         if (subMainMenuCanvas != null)
    155.         {
    156.             subMainMenuCanvas.SetActive(true);
    157.         }
    158.     }
    159.  
    160. [B]public IEnumerator SetResolution (bool callFSMode)
    161.     {
    162.         //Debug.LogError(resDropdown.value);
    163.         //Debug.LogError(resDropdownTMPro.value);
    164.         //Resolution res = resolutions[resDropdown.value + 1];
    165.         if (resDropdownTMPro != null && resolutions != null)
    166.         {
    167.             yield return new WaitForSeconds(0.5f);
    168.             Resolution res = Screen.currentResolution;
    169.  
    170.             int currentResolutionIndex = 0;
    171.             for (int i = 1; i < resolutions.Length; i++)
    172.             {
    173.                 //string option = resolutions[i].width + " x " + resolutions[i].height;
    174.                 if (resolutions[i].width == Screen.width && resolutions[i].height == Screen.height)
    175.                 {
    176.                     currentResolutionIndex = i;
    177.                     break;
    178.                     //Debug.LogError(i);
    179.                 }
    180.             }
    181.             resDropdownTMPro.value = currentResolutionIndex - 1;
    182.             //Screen.SetResolution(res.width, res.height, Screen.fullScreen);
    183.             if (res.width > 4096 || res.height > 3750)
    184.             {
    185.                 myScroller.ResolutionChangeFunc(true);
    186.             }
    187.             else
    188.             {
    189.                 myScroller.ResolutionChangeFunc(false);
    190.             }
    191.  
    192.             if (callFSMode)
    193.             {
    194.                 SetFullscreenMode();
    195.             }
    196.  
    197.  
    198.  
    199.             //PlayerPrefs.SetInt("CurrentResolution", resDropdown.value);
    200.             resDropdownTMPro.RefreshShownValue();
    201.             PlayerPrefs.SetInt("CurrentResolution", resDropdownTMPro.value);
    202.         }
    203.     }
    204.  
    205. public void SetResolution()
    206.     {
    207.         //Debug.LogError(resDropdown.value);
    208.         //Debug.LogError(resDropdownTMPro.value);
    209.         //Resolution res = resolutions[resDropdown.value + 1];
    210.         if (resDropdownTMPro != null && resolutions != null)
    211.         {
    212.             Resolution res = resolutions[resDropdownTMPro.value + 1];
    213.             Screen.SetResolution(res.width, res.height, Screen.fullScreen);
    214.             if (res.width > 4096 || res.height > 3750)
    215.             {
    216.                 myScroller.ResolutionChangeFunc(true);
    217.             }
    218.             else
    219.             {
    220.                 myScroller.ResolutionChangeFunc(false);
    221.             }
    222.          
    223.             //SetFullscreenMode();
    224.          
    225.             //PlayerPrefs.SetInt("CurrentResolution", resDropdown.value);
    226.             PlayerPrefs.SetInt("CurrentResolution", resDropdownTMPro.value);
    227.         }
    228.     }
    229.  
    230.  
    231. public void SetFullscreenMode()
    232.     {
    233.         //if(fsDropdown.options[fsDropdown.value].text == "Fullscreen Window")
    234.         if(fsDropdownTMPro.options[fsDropdownTMPro.value].text == "Fullscreen Window")
    235.         {
    236.             Screen.fullScreenMode = FullScreenMode.FullScreenWindow;
    237.             Debug.Log("Fullscreen Window");
    238.             Screen.fullScreen = true;
    239.             PlayerPrefs.SetInt("SelectedFullscreenMode", 0);
    240.         }
    241.         //else if(fsDropdown.options[fsDropdown.value].text == "Exclusive Fullscreen")
    242.         else if (fsDropdownTMPro.options[fsDropdownTMPro.value].text == "Exclusive Fullscreen")
    243.         {
    244.             Screen.fullScreenMode = FullScreenMode.ExclusiveFullScreen;
    245.             Debug.Log("Exclusive Fullscreen");
    246.             Screen.fullScreen = true;
    247.             PlayerPrefs.SetInt("SelectedFullscreenMode", 1);
    248.         }
    249.         //else if (fsDropdown.options[fsDropdown.value].text == "Maximized Window")
    250.         else if (fsDropdownTMPro.options[fsDropdownTMPro.value].text == "Maximized Window")
    251.         {
    252. #if UNITY_STANDALONE_OSX
    253.             Screen.fullScreenMode = FullScreenMode.MaximizedWindow;
    254.             Debug.Log("Maximized Window");
    255.             //Screen.fullScreen = false;
    256.             PlayerPrefs.SetInt("SelectedFullscreenMode", 2);
    257. #else
    258.             Debug.Log("Not on MAC/OSX build.  Maximized Window menu option should do nothing.");
    259. #endif
    260.         }
    261.         //else if (fsDropdown.options[fsDropdown.value].text == "Windowed")
    262.         else if (fsDropdownTMPro.options[fsDropdownTMPro.value].text == "Windowed")
    263.         {
    264.             Screen.fullScreenMode = FullScreenMode.Windowed;
    265.             Debug.Log("Windowed");
    266.             //Screen.fullScreen = false;
    267.             PlayerPrefs.SetInt("SelectedFullscreenMode", 3);
    268.         }
    269.         StartCoroutine(SetResolution(false));
    270.  
    271.     }[/B]
    Edit: Nvm, I changed my code to make sure windowed resolutions are stored separately and must be smaller than the biggest fullscreen resolution. Seems to work.
     
    Last edited: Jun 25, 2023
  2. ObsidianSpire

    ObsidianSpire

    Joined:
    Sep 13, 2013
    Posts:
    48
    Bump. Still looking for answers if anyone has experience with setting resolutions like this.