- Home /
Question by
Project Zeta · Dec 02, 2012 at 10:29 PM ·
playersoundplayerprefsgetprefs
PlayerPrefs - Sound slider values
I am currently working on a menu for my game, and the sound options screen works for the game, however I would like to have the sound values saved for the next time a player starts the game, instead of them having to change the sound slider everytime.
Currently, I have the sliders set up like this:
static var musicVolume: float = 100;
var master : float = 100;
var minSound : float = 0;
var maxSound : float = 100;
function Sound()
{
//GUILayout variables not related to topic
GUILayout.BeginArea(Rect(hPosition, vPosition, buttonWidth, 600));
//sliders
GUILayout.Label("Master");
master = GUILayout.Horizontal (master, minSound, maxSound);
musicVolume = GUILayout.HorizontalSlider(musicVolume, minSound, maxSound);
if(GUILayout.Button("Back to Options")
{
PlayerPrefs.SetFloat("Master", master);
PlayerPrefs.SetFloat("Music", musicVolume);
GUILayout.EndArea()
}
There are a few others things, executing Sound() etc.
My question is: I have the volume values being saved when the user clicks "Back to Options". However, how would it be possible to load the values onto the sliders after the user closes and opens the game later?
Comment
Best Answer
Answer by Caiuse · Dec 02, 2012 at 10:46 PM
You could just setup the slider variables based off the float you saved to originally saved in PlayerPrefs, for example:
function Start(){
if(PlayerPrefs.HasKey("Master"){
musicVolume = PlayerPrefs.GetFloat("Master");
}
}
It's better to use a default value for GetFloat rather than checking for keys.
function Start() {
// Set default volume to 1.0 if this preference hasn't been saved before
musicVolume = PlayerPrefs.GetFloat("$$anonymous$$aster", 1.0);
}
Your answer
Follow this Question
Related Questions
Read only playerprefs? 1 Answer
Organizing Player Prefs? 1 Answer
Triggering random sound on player 1 Answer
PlayerPrefs question 0 Answers