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_.width + " x " + resolutions*.height;*_
options.Add(option);
if(resolutions_.width == Screen.currentResolution.width && resolutions*.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!_
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_.width + " x " + resolutions*.height;*_
options.Add(option);
if (Mathf.Approximately(resolutions_.width, selectedResolution.width) && Mathf.Approximately(resolutions*.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.
@Hellium thanks for your time! I tried with your code but it doesn’t seem to work…