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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

"Resolution" switches back to FullScreen and NativeResolution

Discussion in 'Windows' started by OlafsOsh, Feb 16, 2021.

  1. OlafsOsh

    OlafsOsh

    Joined:
    Jul 10, 2018
    Posts:
    61
    Guys. I have a picle and my eyes are squared already.

    Everything works - within Menu I can switch the Resolution and FullScreen On/Off. The problem is, that upon new load, the game does show a right size window at first [the one I chose at previous launch of the game - smaller, non-FullScreen], but then switches back to Native Res FullScreen.

    And I have tried gazillion things... I just cannot see straight anymore.. Help. Sorry for the commented out mess - just trying different things...

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6. using System.Linq;
    7.  
    8. public class SettingsMenu : MonoBehaviour
    9. {
    10.     public TMP_Dropdown resolutionDropdown;
    11.     Resolution[] resolutions;
    12.     Resolution selectedResolution; //added
    13.     bool isFullScreen;
    14.    
    15.     private const string resolutionWidthPlayerPrefKey = "ResolutionWidth";//added
    16.     private const string resolutionHeightPlayerPrefKey = "ResolutionHeight";//added
    17.     private const string isFullScreenPlayerPrefKey = "isFullScreenPlayerPrefKey"; //2
    18.     private void Awake()
    19.     {
    20.         //    Invoke("SetResolution", 0f);
    21.         //    Invoke("SetFullScreen", 0);
    22.         //PlayerPrefs.GetInt("isFullscreen", (isFullScreen ? 1 : 0));
    23.     }
    24.     private void Start()
    25.     {
    26.         resolutions = Screen.resolutions.Select(resolution => new Resolution { width = resolution.width, height = resolution.height }).Distinct().ToArray();
    27.         resolutionDropdown.ClearOptions();
    28.         List<string> options = new List<string>();
    29.        
    30.         int currentResolutionIndex = 0;
    31.         for (int i = 0; i < resolutions.Length; i++)
    32.         {
    33.             string option = resolutions[i].width + " x " + resolutions[i].height;
    34.             options.Add(option);
    35.  
    36.             if (resolutions[i].width == Screen.currentResolution.width && resolutions[i].height == Screen.currentResolution.height)
    37.             {
    38.                 currentResolutionIndex = i;
    39.             }
    40.         }
    41.         resolutionDropdown.AddOptions(options);
    42.         resolutionDropdown.value = currentResolutionIndex;
    43.         resolutionDropdown.RefreshShownValue();
    44.  
    45.        
    46.         //selectedResolution = new Resolution(); //added 9
    47.         selectedResolution.width = PlayerPrefs.GetInt(resolutionWidthPlayerPrefKey, Screen.currentResolution.width); //added 13
    48.         selectedResolution.height = PlayerPrefs.GetInt(resolutionHeightPlayerPrefKey, Screen.currentResolution.height); //added 13
    49.         //Screen.fullScreen = isFullScreen; //12
    50.         //PlayerPrefs.GetInt(isFullScreenPlayerPrefKey); //12 14
    51.         //Screen.fullScreen = isFullScreen;//7 8
    52.         // PlayerPrefs.GetInt("isFullScreenPlayerPrefKey", (isFullScreen ? 1 : 0));//7 11 isFullScreenPlayerPrefKey 13off
    53.         //Invoke("SetFullScreen", 0); //6
    54.         //Invoke("SetFullScreen", 0);
    55.     }
    56.  
    57.     public void SetResolution(int resolutionIndex)
    58.     {
    59.         Resolution resolution = resolutions[resolutionIndex];
    60.         Screen.SetResolution(resolution.width, resolution.height, Screen.fullScreen);
    61.  
    62.         PlayerPrefs.SetInt(resolutionWidthPlayerPrefKey, resolution.width);//added
    63.         PlayerPrefs.SetInt(resolutionHeightPlayerPrefKey, resolution.height);//added
    64.         //Invoke("SetFullScreen", 0f); //added
    65.     }
    66.     public void SetFullScreen(bool isFullScreen)
    67.     {
    68.         Screen.fullScreen = isFullScreen;
    69.         PlayerPrefs.SetInt("isFullScreenPlayerPrefKey", (isFullScreen ? 1 : 0)); //added1520 1550off 2 4 7get 11 isFullScreenPlayerPrefKey
    70.  
    71.         //PlayerPrefs.SetInt("isFullscreen", (isFullScreen ? 1 : 0)); //added 1550on 2
    72.         // isFullScreen = (PlayerPrefs.GetInt("isFullscreen") != 0); //added1449 1542 3
    73.  
    74.     }
    75.  
    76. }
    77.  


    upload_2021-2-16_21-7-32.png