- Home /
How to save the resolution of screen between scenes
Hi, i'm doing the options menu for the game (the resolutions) and i now have all the code that i need but i don't know how to save it with the player prefs, basically I would like to save the resolution of the dropdown and the state of fullscreen between scenes. As now the code starts with a resolution but it doesn't save the selected resolution (the text of the dropdown options, the resolutions itself remains between scenes) or the state toggle of the fullscreen when I change from the main menu to the game scene.
using UnityEngine; using UnityEngine.UI;
public class setting_menu : MonoBehaviour
{ List widths = new List () {3840, 2560, 1920}; List heights = new List () {2160, 1440, 1080};
void Awake()
{
SetResolution(1);
}
public void SetFullscreen(bool isFullscreen)
{
Screen.fullScreen = isFullscreen;
}
public void SetResolution(int resolutionIndex)
{
bool fullScreen = Screen.fullScreen;
int width = widths[resolutionIndex];
int height = heights[resolutionIndex];
Screen.SetResolution(width, height, fullScreen);
}
}
I did this following a tutorial on Youtube Thanks for the help!
Answer by Patrickmol · Jun 16, 2021 at 07:06 AM
public static List widths = new List () {3840, 2560, 1920};
public static List heights = new List () {2160, 1440, 1080};
with static you will be able to keep the value when changing scene
Thanks for the help! Any idea of how to make the dropdown change the text of the option when I change the resolution? As now the code change the resolution and it stays between scenes but the dropdown options remains the same between scenes, I mean it doesn't change base on the resolution selected.
Answer by unwshd_elend · Jun 16, 2021 at 07:28 AM
saving it with player prefs is not any different than saving other values , you simply define one key for width and one for height and save the values there , better than using static values since they only remain in current game session so it will something like this :
private const string SCREEN_WIDTH = "sc_width";
private const string SCREEN_HEIGHT = "sc_height";
PlayerPrefs.SetInt(SCREEN_WIDTH , 3840);
PlayerPrefs.SetInt(SCREEN_HEIGHT, 2160);
Thanks for the help! Any idea of how to make the dropdown change the text of the option when I change the resolution? As now the code change the resolution and it stays between scenes but the dropdown options remains the same between scenes, I mean it doesn't change base on the resolution selected.