Save Settings
Part of the Building Real Games section of Coddy's Unity journey. Lesson 25 of 56.
A setting is chosen once and expected back in every game after, so it belongs in PlayerPrefs. When the game starts, load the setting, put it on its control so the Settings screen shows it, and apply it to the game. When the player changes the control, apply the new value and save it. This toggle (using UnityEngine.UI;) shows or hides the on-screen timer, and since PlayerPrefs has no bool, it stores 1 for on and 0 for off:
public Toggle timerToggle;
public GameObject timerText;
void Start()
{
bool show = PlayerPrefs.GetInt("ShowTimer", 1) == 1;
timerToggle.isOn = show;
timerText.SetActive(show);
timerToggle.onValueChanged.AddListener(OnTimerToggled);
}
void OnTimerToggled(bool on)
{
timerText.SetActive(on);
PlayerPrefs.SetInt("ShowTimer", on ? 1 : 0);
PlayerPrefs.Save();
}onValueChanged passes the control's new value to the listener: a bool for a Toggle, a float for a Slider, whose position is slider.value. Setting isOn or value from code changes the control the way a player does, so when the value changes it calls the listeners too. That is why the listener is added after the load: added first, it would run on every start and save the loaded value straight back. SetIsOnWithoutNotify and SetValueWithoutNotify set a control without calling any listener.
The default decides what a new player gets. GetInt("ShowTimer", 1) shows the timer until the player turns it off; a default of 0 would hide it from everyone who never opened the settings. A float setting loads the same way with PlayerPrefs.GetFloat(key, defaultValue): GetFloat("MouseSensitivity", 1.5f) gives a new player 1.5.
Challenge
MediumThe Settings screen has a volume slider from 0 to 1 and a text that shows it as a percentage. Make the volume a saved setting, under the key Volume:
- A new player starts at 0.8, shown as Volume: 80%.
- The volume applies to the whole game through
AudioListener.volume, Unity's master volume from 0 (silent) to 1 (full), which scales every sound in the game. - When the game starts, the saved volume is applied, and the slider and the text show it.
- When the player moves the slider, the new volume is applied, the text follows and the volume is saved.
ShowVolume(float volume) already writes the text.
Press Play and move the slider, then Stop and Play again, then Check.
Try it yourself
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class VolumeSetting : MonoBehaviour
{
public Slider slider;
public TMP_Text volumeText;
void Start()
{
// Load the saved volume, apply and show it, then listen to the slider
}
void OnVolumeChanged(float value)
{
// Apply, show and save the new volume
}
void ShowVolume(float volume)
{
volumeText.text = "Volume: " + Mathf.RoundToInt(volume * 100) + "%";
}
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Building Real Games
2Buttons and Screens
Buttons and onClickLambdasWiring in the InspectorPanels On and OffHealth BarsRecap: Upgrade Shop5Saving with PlayerPrefs
Save a NumberLoad with a DefaultHigh ScoreSave SettingsRecap: Coins in the Bank3The Game Manager
One Script Runs the GameThe SingletonGame StatesSwitching on the StateRecap: Round Manager6Project: Endless Runner
Three LanesObstacles Ahead9The New Input System
Two Input SystemsKeyboard and MousePressed This FrameInput ActionsRecap: Input Swap