- Home /
how to save the resolutions for the options menu
Hi, thanks a lot in advice 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 fullscreen to make that when I select a resolution and then I close the game, when I reopen the game everything is saved
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class ResolutionMenu : MonoBehaviour {
public Dropdown resolutionDropdown;
Resolution[] resolutions;
int currentResolutionIndex = 0;
void Start()
{
resolutions = Screen.resolutions;
resolutionDropdown.ClearOptions();
List<string> options = new List<string>();
for (int i = 0; i < resolutions.Length; i++)
{
string option = resolutions[i].width + " x " + resolutions[i].height;
options.Add(option);
if(resolutions[i].width == Screen.currentResolution.width && resolutions[i].height == Screen.currentResolution.height)
{
currentResolutionIndex = i;
}
}
resolutionDropdown.AddOptions(options);
resolutionDropdown.value = currentResolutionIndex;
resolutionDropdown.RefreshShownValue();
}
public void SetFullscreen(bool isFullscreen)
{
Screen.fullScreen = isFullscreen;
}
public void SetResolution(int resolutionIndex)
{
Resolution resolution = resolutions[resolutionIndex];
Screen.SetResolution(resolution.width, resolution.height, Screen.fullScreen);
}
} this is the code(brackeys) and basically i want to save the fullscreen toggle and the current resolution of the dropdown thank a lot in advice for your time!
Answer by Hellium · Mar 02, 2020 at 12:01 PM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ResolutionMenu : MonoBehaviour
{
private const string resolutionWidthPlayerPrefKey = "ResolutionWidth";
private const string resolutionHeightPlayerPrefKey = "ResolutionHeight";
private const string resolutionRefreshRatePlayerPrefKey = "RefreshRate";
private const string fullScreenPlayerPrefKey = "FullScreen";
public Toggle fullScreenToggle;
public Dropdown resolutionDropdown;
Resolution[] resolutions;
Resolution selectedResolution;
void Start()
{
resolutions = Screen.resolutions;
LoadSettings();
CreateResolutionDropdown();
fullScreenToggle.onValueChanged.AddListener(SetFullscreen);
resolutionDropdown.onValueChanged.AddListener(SetResolution);
}
private void LoadSettings()
{
selectedResolution = new Resolution();
selectedResolution.width = PlayerPrefs.GetInt(resolutionWidthPlayerPrefKey, Screen.currentResolution.width);
selectedResolution.height = PlayerPrefs.GetInt(resolutionHeightPlayerPrefKey, Screen.currentResolution.height);
selectedResolution.refreshRate = PlayerPrefs.GetInt(resolutionRefreshRatePlayerPrefKey, Screen.currentResolution.refreshRate);
fullScreenToggle.isOn = PlayerPrefs.GetInt(fullScreenPlayerPrefKey, Screen.fullScreen ? 1 : 0) > 0;
Screen.SetResolution(
selectedResolution.width,
selectedResolution.height,
fullScreenToggle.isOn
);
}
private void CreateResolutionDropdown()
{
resolutionDropdown.ClearOptions();
List<string> options = new List<string>();
int currentResolutionIndex = 0;
for (int i = 0; i < resolutions.Length; i++)
{
string option = resolutions[i].width + " x " + resolutions[i].height;
options.Add(option);
if (Mathf.Approximately(resolutions[i].width, selectedResolution.width) && Mathf.Approximately(resolutions[i].height, selectedResolution.height))
{
currentResolutionIndex = i;
}
}
resolutionDropdown.AddOptions(options);
resolutionDropdown.value = currentResolutionIndex;
resolutionDropdown.RefreshShownValue();
}
public void SetFullscreen(bool isFullscreen)
{
Screen.fullScreen = isFullscreen;
PlayerPrefs.SetInt(fullScreenPlayerPrefKey, isFullscreen ? 1 : 0);
}
public void SetResolution(int resolutionIndex)
{
selectedResolution = resolutions[resolutionIndex];
Screen.SetResolution(selectedResolution.width, selectedResolution.height, Screen.fullScreen);
PlayerPrefs.SetInt(resolutionWidthPlayerPrefKey, selectedResolution.width);
PlayerPrefs.SetInt(resolutionHeightPlayerPrefKey, selectedResolution.height);
PlayerPrefs.SetInt(resolutionRefreshRatePlayerPrefKey, selectedResolution.refreshRate);
}
}
You will also have to remove the callbacks you have specified through the inspector of the dropdown and the toggle. All is set up by code.
Hi! Thanks for the help with this code, I wanted to asked you how can I define a serie of specific resolutions, I mean I don't want all the screen resolutions to be avaliable... I just want Full HD (1920x1080), 2K and 4K. And also to be that the text of the option dropdown, not the pixels just the name like 2K and 4K...
Thanks for the help! @Hellium
Code not tested, written on a simple notepad, soo I don't even know if it compiles
public struct Resolution
{
public readonly string name;
public readonly int width;
public readonly int height;
public readonly int refreshRate;
public Resolution(string n, int w, int h, int rr)
{
name = n;
width = w;
height = h;
refreshRate = rr;
}
}
public class ResolutionMenu : MonoBehaviour
{
private const string resolutionNamePlayerPrefKey = "ResolutionWidth";
private const string fullScreenPlayerPrefKey = "FullScreen";
public Toggle fullScreenToggle;
public Dropdown resolutionDropdown;
Resolution[] resolutions;
Resolution selectedResolution;
void Start()
{
resolutions = new Resolution[]
{
new Resolution("Custom", Screen.currentResolution.width, Screen.currentResolution.height, Screen.currentResolution.refreshRate),
new Resolution("Full HD", 1920, 1080, 60),
new Resolution("4K", 3840, 2160, 60),
// ....
};
LoadSettings();
CreateResolutionDropdown();
fullScreenToggle.onValueChanged.AddListener(SetFullscreen);
resolutionDropdown.onValueChanged.AddListener(SetResolution);
}
private void LoadSettings()
{
int index = Mathf.Max(
System.Array.FindIndex(resolutions, resolution => resolution.name == PlayerPrefs.GetString(resolutionNamePlayerPrefKey)),
0 // 0 = Index of custom resolution
);
selectedResolution = resolutions[index];
fullScreenToggle.isOn = PlayerPrefs.GetInt(fullScreenPlayerPrefKey, Screen.fullScreen ? 1 : 0) > 0;
Screen.SetResolution(
selectedResolution.width,
selectedResolution.height,
fullScreenToggle.isOn,
selectedResolution.refreshRate
);
}
private void CreateResolutionDropdown()
{
resolutionDropdown.ClearOptions();
List<string> options = new List<string>();
int currentResolutionIndex = 0;
for (int i = 0; i < resolutions.Length; i++)
{
options.Add(resolutions[i].name);
if (resolutions[i].width == selectedResolution.width && resolutions[i].height == selectedResolution.height)
{
currentResolutionIndex = i;
}
}
resolutionDropdown.AddOptions(options);
resolutionDropdown.value = currentResolutionIndex;
resolutionDropdown.RefreshShownValue();
}
// SetFullscreen(bool isFullscreen) ...
// SetResolution(int resolutionIndex) ...
}
Thanks for the help! the code is working well but still if I selected the 4K on the main menu and then go to the game and open de setting screen on that scene I get that the selected resolution is other like the Custom :( and on the dropdown the custom is showing as an option and won't like that to happen I tried to erase that line of code but the I won't work.
Thanks again for your advice and time, I really appreciate your help! @Hellium