unity playerprifs code example

Example 1: how to use playerprefs in unity

SAVING
 
 PlayerPrefs.SetInt("score",5);
 PlayerPrefs.SetFloat("volume",0.6f);
 PlayerPrefs.SetString("username","John Doe");
 PlayerPrefs.Save();
 
// If you need boolean value:

 bool val = true;
 PlayerPrefs.SetInt("PropName", val ? 1 : 0);
 PlayerPrefs.Save();

 READING

 int score = PlayerPrefs.GetInt("score");
 float volume = PlayerPrefs.GetFloat("volume");
 string player = PlayerPrefs.GetString("username");
 
 bool val = PlayerPrefs.GetInt("PropName") == 1 ? true : false;

Example 2: playerprefs unity

//Variables of different type
	int number = 18;
	float decimal = 1.84f;
	string word = "Jeff";

//Setting each PlayerPref to a Variable, with .Set and the type of Variable its set too (.SetFloat)
	PlayerPrefs.SetInt("Age", number);
	PlayerPrefs.SetFloat("Height", decimal);
	PlayerPrefs.SetString("Name", word);

//Displaying the PlayerPref in Console, with .Get and its type of data (.GetString)
	Debug.Log(PlayerPrefs.GetInt("Age"));
	Debug.Log(PlayerPrefs.GetFloat("Height", 1.5));// Adding a default Value incase the PlayerPref isn't storing any data
	Debug.Log(PlayerPrefs.GetString("Name"));
/*Desplayed in Console:
	18
	1.84
	Jeff
*/

//Reseting data in one PlayerPref
	PlayerPrefs.DeleteKey("Height");
//Reseting data in all PlayerPrefs
P	layerPrefs.DeleteAll();