Search Unity

Resolution change won't stick.

Discussion in 'Scripting' started by screenname_taken, Jul 27, 2017.

  1. screenname_taken

    screenname_taken

    Joined:
    Apr 8, 2013
    Posts:
    663
    Hi, I wanted to add a menu for selecting a custom resolution in-game.
    My script works in populating the dropdown menu and if i select a resolution, i can see in the registry that it writes it correctly.
    Then if i close my test build and start it again, the game starts in the resolution that i had previously selected, and once it fully loads the scene is in the scene goes to the native resolution instead!
    I don't have the "default is native res" in the build options enabled.
    Is there something that i'm missing in the setup?

    I can't see anywhere in my code that says "go to native res".

    I have PlayerPrefs.SetInt("width", resolutionList[resolutionList.Length-1].width); (and the same for height) and in the registry, i see that both are set to the width resolution! including the height.

    It remembers the full screen setting, but not the resolution.
    Is it a known bug or issue?
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
  3. screenname_taken

    screenname_taken

    Joined:
    Apr 8, 2013
    Posts:
    663
    No error. It does change the resolution and then saves it in the registry.
    The issue is that when i load the game back, it momentarily stays in the correct, previous resolution selected, but when it loads fully, it goes directly back to native res.

    This script executes first, before everything else, to setup some stuff.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class initSetup : MonoBehaviour {
    4.  
    5.     void Awake() {
    6.         //Lets init some stuff!!!
    7.         if (!PlayerPrefs.HasKey("firstboot")) {
    8.             PlayerPrefs.SetInt("firstboot", 1);
    9.             PlayerPrefs.SetInt("ambientOcclusion", 1);
    10.             PlayerPrefs.SetInt("AA", 1);
    11.             PlayerPrefs.SetInt("bloom", 1);
    12.             PlayerPrefs.SetInt("SSreflections", 1);
    13.             PlayerPrefs.SetInt("quality", 2);
    14.             PlayerPrefs.SetInt("fullScreen", 1);
    15.             PlayerPrefs.SetInt("limiter", 1);
    16.             QualitySettings.SetQualityLevel(2);
    17.             PlayerPrefs.SetInt("width", Screen.width);
    18.             PlayerPrefs.SetInt("height", Screen.height);
    19.             Screen.fullScreen = true;
    20.             //Application.targetFrameRate = 60;
    21.         }
    22.         Application.targetFrameRate = -1;
    23.         QualitySettings.SetQualityLevel(PlayerPrefs.GetInt("quality"));
    24.         QualitySettings.vSyncCount = PlayerPrefs.GetInt("limiter");    
    25.     }
    26. }
    Then comes this script that checks options, for the times after the 1st time you run it.
    Code (CSharp):
    1. using UnityEngine.PostProcessing;
    2. using UnityEngine;
    3.  
    4. public class cameraOptions : MonoBehaviour {
    5.     public PostProcessingProfile postProcessScriptCache;
    6.  
    7.     void Start () {
    8.         postProcessScriptCache = GetComponent<PostProcessingBehaviour>().profile;
    9.         if (PlayerPrefs.GetInt("ambientOcclusion") == 1) {
    10.             postProcessScriptCache.ambientOcclusion.enabled = true;
    11.         }
    12.         else { postProcessScriptCache.ambientOcclusion.enabled = false; }
    13.  
    14.         if (PlayerPrefs.GetInt("AA") == 1) {
    15.             postProcessScriptCache.antialiasing.enabled = true;
    16.         }
    17.         else { postProcessScriptCache.antialiasing.enabled = false; }
    18.  
    19.         if (PlayerPrefs.GetInt("bloom") == 1) {
    20.             postProcessScriptCache.bloom.enabled = true;
    21.         }
    22.         else { postProcessScriptCache.bloom.enabled = false; }
    23.  
    24.         if (PlayerPrefs.GetInt("SSreflections") == 1) {
    25.             postProcessScriptCache.screenSpaceReflection.enabled = true;
    26.         }
    27.         else { postProcessScriptCache.screenSpaceReflection.enabled = false; }
    28.  
    29.         bool fullscreen;
    30.         if (PlayerPrefs.GetInt("fullScreen") == 1) {
    31.             fullscreen = true;
    32.         }
    33.         else {
    34.             fullscreen = false;
    35.         }
    36.         Screen.fullScreen = fullscreen;
    37.  
    38.         QualitySettings.SetQualityLevel(PlayerPrefs.GetInt("quality"));
    39.         int fpslimit = PlayerPrefs.GetInt("limiter");
    40.         switch (fpslimit) {
    41.             case 0:
    42.                 //      Application.targetFrameRate = -1; //unlimited.
    43.                 QualitySettings.vSyncCount = 0;
    44.                 break;
    45.             case 1:
    46.                 //      Application.targetFrameRate = Monitor Refresh;
    47.                 QualitySettings.vSyncCount = 1;
    48.                 break;
    49.             case 2:
    50.                 //       Application.targetFrameRate = half rate;
    51.                 QualitySettings.vSyncCount = 2;
    52.                 break;
    53.         }
    54.  
    55.         if (PlayerPrefs.HasKey("firstboot")) {
    56.             Screen.SetResolution(PlayerPrefs.GetInt("width"), PlayerPrefs.GetInt("height"), Screen.fullScreen);
    57.         }
    58.     }
    59. }
    The last piece is the script that is on the "options" menu to allow you to change your settings.
    Code (CSharp):
    1. using UnityEngine.PostProcessing;
    2. using UnityEngine;
    3.  
    4. public class cameraOptions : MonoBehaviour {
    5.     public PostProcessingProfile postProcessScriptCache;
    6.  
    7.     void Start () {
    8.         postProcessScriptCache = GetComponent<PostProcessingBehaviour>().profile;
    9.         if (PlayerPrefs.GetInt("ambientOcclusion") == 1) {
    10.             postProcessScriptCache.ambientOcclusion.enabled = true;
    11.         }
    12.         else { postProcessScriptCache.ambientOcclusion.enabled = false; }
    13.  
    14.         if (PlayerPrefs.GetInt("AA") == 1) {
    15.             postProcessScriptCache.antialiasing.enabled = true;
    16.         }
    17.         else { postProcessScriptCache.antialiasing.enabled = false; }
    18.  
    19.         if (PlayerPrefs.GetInt("bloom") == 1) {
    20.             postProcessScriptCache.bloom.enabled = true;
    21.         }
    22.         else { postProcessScriptCache.bloom.enabled = false; }
    23.  
    24.         if (PlayerPrefs.GetInt("SSreflections") == 1) {
    25.             postProcessScriptCache.screenSpaceReflection.enabled = true;
    26.         }
    27.         else { postProcessScriptCache.screenSpaceReflection.enabled = false; }
    28.  
    29.         bool fullscreen;
    30.         if (PlayerPrefs.GetInt("fullScreen") == 1) {
    31.             fullscreen = true;
    32.         }
    33.         else {
    34.             fullscreen = false;
    35.         }
    36.         Screen.fullScreen = fullscreen;
    37.  
    38.         QualitySettings.SetQualityLevel(PlayerPrefs.GetInt("quality"));
    39.         int fpslimit = PlayerPrefs.GetInt("limiter");
    40.         switch (fpslimit) {
    41.             case 0:
    42.                 //      Application.targetFrameRate = -1; //unlimited.
    43.                 QualitySettings.vSyncCount = 0;
    44.                 break;
    45.             case 1:
    46.                 //      Application.targetFrameRate = Monitor Refresh;
    47.                 QualitySettings.vSyncCount = 1;
    48.                 break;
    49.             case 2:
    50.                 //       Application.targetFrameRate = half rate;
    51.                 QualitySettings.vSyncCount = 2;
    52.                 break;
    53.         }
    54.  
    55.         if (PlayerPrefs.HasKey("firstboot")) {
    56.             Screen.SetResolution(PlayerPrefs.GetInt("width"), PlayerPrefs.GetInt("height"), Screen.fullScreen);
    57.         }
    58.     }
    59. }
     
  4. Talasas

    Talasas

    Joined:
    May 14, 2011
    Posts:
    8
    Did you ever solve this issue? I am seeing the same problem and it's got me stumped!
     
  5. JackReivaj

    JackReivaj

    Joined:
    Feb 7, 2021
    Posts:
    33
    Anyone solve this problem? I'm having the same problem :(
     
  6. gewl

    gewl

    Joined:
    May 19, 2016
    Posts:
    95
    I'm experiencing this on Unity 2020.3 as well.
     
  7. TequilaNoAim

    TequilaNoAim

    Joined:
    Jul 23, 2020
    Posts:
    1
    I have the same issue. I even noticed that the problem occurs only if the game isn't in fullscreen mode, so I think it's a bug that reset the resolution back to native if the window isn't in fullscreen
     
    LeandroExHuMeD likes this.