Save Settings
Part of the Building Real Games section of Coddy's Unity journey. Lesson 25 of 60.
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. A volume setting goes on AudioListener.volume, Unity's master volume from 0 (silent) to 1 (full), which scales every sound in the game.
Challenge
MediumMake the volume slider a saved setting under the key Volume.
- In
Start(), loadPlayerPrefs.GetFloat("Volume", 0.8f), put it onslider.valueandAudioListener.volume, and callShowVolume. - Then add
slider.onValueChanged.AddListener(OnVolumeChanged);. - In
OnVolumeChanged, applyvaluetoAudioListener.volume, show it, and save it withPlayerPrefs.SetFloatandPlayerPrefs.Save().
Press Play and move the slider, then Stop and Play: it stays where you left it. 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) + "%";
}
}
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 SwapPractice on your own: Unity playground