- Home /
How to make my game settings save?
I've got a working settings menu, with a volume slider, resolution dropdown, etc. The problem is the settings don't get saved, when transitioning to a different scene, or when restarting the game. I know I have to use PlayerPrefs, but I just don't know how to implement it into my code, would appreciate if anyone could help. Here is my settings menu code:
public AudioMixer audioMixer;
Resolution[] resolutions;
public TMPro.TMP_Dropdown resolutionDropdown;
void Start()
{
resolutions = Screen.resolutions;
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 (resolutions[i].width == Screen.currentResolution.width &&
resolutions[i].height == Screen.currentResolution.height)
{
currentResolutionIndex = i;
}
}
resolutionDropdown.AddOptions(options);
resolutionDropdown.value = currentResolutionIndex;
resolutionDropdown.RefreshShownValue();
}
public void setResolution(int resolutionIndex)
{
Resolution resolution = resolutions[resolutionIndex];
Screen.SetResolution(resolution.width, resolution.height, Screen.fullScreen);
}
public void setVolume(float volume)
{
audioMixer.SetFloat("volume", volume);
}
public void setQuality(int qualityIndex)
{
QualitySettings.SetQualityLevel(qualityIndex);
}
public void setFullscreen(bool isFullscreen)
{
Screen.fullScreen = isFullscreen;
}
People who actually need some useful advice, this is a perfect video: https://www.youtube.com/watch?v=rtpHU1kfabI
Answer by highpockets · Aug 20, 2020 at 11:16 PM
You don’t have to use player prefs, but yes that is Unity’s built in solution and it is quite straight forward to use.
Anyhow, I don’t see any attempt to actually use PlayerPrefs inside your code, but if you want to save a string, for example, you would do the following:
string myString = “Howdy”;
//Set the string
PlayerPrefs.SetString( “MY_STRING_KEY”, myString);
//Get the string
string myStringAfterItWasSaved = PlayerPrefs.GetString( “MY_STRING_KEY” );
//myStringAfterItWasSaved == “Howdy”
Probably an important thing i forgot to mention is, that i'm new and that this code is from the internet, and I barely know whats happening in there, so even with the example you gave me I still don't know how and what to do :D
I would suggest you do some tutorials to get the basics down and then this will be a piece of cake. Good luck
Your answer
Follow this Question
Related Questions
Unity problem when i convert playerprefs save to the standard file io save 0 Answers
PlayerPrefs.GetString not saving past values 0 Answers
Saving Post Processing as a graphic option 1 Answer
options menu UI doesnt save when switching scenes 1 Answer
Need help with using Player Prefs to save number of coins collected. 1 Answer