I am having a problem with the resolution. The resolution is not saved. When I close the game and reopen it at first it opens fine and suddenly it changes to a default resolution and I have to change it again. I do not understand what is wrong in the code that the resolution is not saved. I already tried a thousand ways and I can’t solve it. Here is the code that I am using to change the resolution and save it:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class LogicResolution : MonoBehaviour
{
public TMP_Dropdown resolutionsDropDown;
public int optionIndex;
void Start()
{
optionIndex = PlayerPrefs.GetInt("resolutionNumber", 2);
resolutionsDropDown.value = optionIndex;
AdjustResolution();
}
public void AdjustResolution()
{
if (resolutionsDropDown.value == 0)
Screen.SetResolution(640, 360, Screen.fullScreen);
else if (resolutionsDropDown.value == 1)
Screen.SetResolution(1280, 720, Screen.fullScreen);
else if (resolutionsDropDown.value == 2)
Screen.SetResolution(1920, 1080, Screen.fullScreen);
else if (resolutionsDropDown.value == 3)
Screen.SetResolution(3840, 2160, Screen.fullScreen);
PlayerPrefs.SetInt("resolutionNumber", resolutionsDropDown.value);
optionIndex = resolutionsDropDown.value;
}
}
The default value is 1280x720 when I actually set it to start with 1920x1080.
AjustarResolucion();
and
AdjustResolution()
?
Sorry, I have the script in Spanish and I translated it into English so that everyone can understand it, I forgot to translate that part, but there are no errors, they are both the same, now I fix it in the original entry.
1 Like
There is a LOT going on in this script that simply doesn’t need to happen.
It all seems (on the surface) okay, but let’s take a look at what you’re doing:
-
Reading from playerprefs into optionIndex
-
Copying it to the dropdown.value (a Unity side thing)
-
calling adjust, which then:
-
reads it back from dropdown four (4) separate times, comparing it each time. (perhaps use a switch statement or an indexed array of resolutions)
-
You unconditionally write it back to playerprefs (why?)
-
You copy it one more time to optionindex (why?)
How about removing PlayerPrefs from your code completely and hiding it in one safe place where you know nobody else can touch it:
Here’s an example of simple persistent values using PlayerPrefs:
Now… on load, do two things:
-
read from the above static playerprefs wrapper
-
act on it and set the resolution
That’s it, nothing more, stop stop stop. Don’t go doing lots and lots of random copyings back and forth.
WHEN you change the dropdown input object, do two things:
-
save the dropdown result to the static playerprefs wrapper
-
act on it the same way you did on startup.
That’s it. No more. Simple, simple, simple.
1 Like
I try to understand what you say but i can’t understand, i saw a lot of videos on youtube of how to use playerprefs and all people do something like i do. I guess I have no choice but to continue investigating. But I started programming only 2 months ago and what you gave me I couldn’t follow :(. Anyway, I appreciate your help.