What is Unity PlayerPrefs? How Is It Used?
What is Unity PlayerPrefs? How Is It Used?
Hello everyone, in this post I will talk about the PlayerPrefs feature in Unity, happy reading.
What is PlayerPrefs?
PlayerPrefs is a structure that allows us to store and save data at a simple level. Of course, we can later retrieve and use this saved data wherever we want.
We generally use PlayerPrefs to store simple data. If I need to give examples of these;
- Keeping the High Score count.
- Keeping the gold count.
- Retrieving data independently from the script when moving from one scene to another.
How to Use PlayerPrefs?
Set Methods - Creating and Editing PlayerPrefs
PlayerPrefs.Set<DataType>("hash", value);
If I need to give an example of how it is used, let's examine the code below.
PlayerPrefs.SetFloat("floatPP", 1.2f); // We created a PlayerPrefs of Float type.
PlayerPrefs.SetInt("integerPP", 10); // We created a PlayerPrefs of Int type.
PlayerPrefs.SetString("stringPP", "UrhobA"); // We created a PlayerPrefs of String type.
If we wanted to edit these, we would also change the value of the data we want to edit and write it the same way. For example;
PlayerPrefs.SetFloat("floatPP", 99.99f); // We created a PlayerPrefs of Float type.
PlayerPrefs.SetInt("integerPP", 99); // We created a PlayerPrefs of Int type.
PlayerPrefs.SetString("stringPP", "www.urhoba.net"); // We created a PlayerPrefs of String type.
Save() Method - Writing PlayerPrefs to Disk
PlayerPrefs.Save();
I think you are wondering why I should use the Save() method, so I will explain right away.
Unity writes the data saved with PlayerPrefs to disk when the "OnApplicationQuit()" function is called; that is, the data is saved to disk just before exiting the game.
In case of exiting the game unexpectedly (crash, phone running out of battery, etc.), the data up to that moment will not be written to disk, so the data will be lost. To prevent this, we use the Save() method.
For an example of its usage, we can use it right below the set methods above.
PlayerPrefs.SetFloat("floatPP", 1.2f); // We created a PlayerPrefs of Float type.
PlayerPrefs.SetInt("integerPP", 10); // We created a PlayerPrefs of Int type.
PlayerPrefs.SetString("stringPP", "UrhobA"); // We created a PlayerPrefs of String type.
PlayerPrefs.Save(); // We wrote the data to disk.
Get Methods - Reading PlayerPrefs Data
PlayerPrefs.Get<DataType>("hash");
It is just like using the set method, the only difference is that here we write "Get" instead of "Set," and we don't write anything for the "value" part. Let's see how we use it in code.
Debug.Log(PlayerPrefs.GetFloat("floatPP").ToString()); // We print a float value to the console.
Debug.Log(PlayerPrefs.GetInt("integerPP").ToString()); // We print an int value to the console.
Debug.Log(PlayerPrefs.GetString("stringPP")); // We print a string value to the console.
float floatValue = PlayerPrefs.GetFloat("floatPP"); // We assign a float value to a float variable.
int intValue = PlayerPrefs.GetInt("integerPP"); // We assign an int value to an int variable.
string stringValue = PlayerPrefs.GetString("stringPP"); // We assign a string value to a string variable.
Delete Methods - Deleting PlayerPrefs Data
- DeleteAll();
- DeleteKey("hash");
DeleteAll() - Deleting All PlayerPrefs
PlayerPrefs.DeleteAll();
If we need to examine how it is used in code;
PlayerPrefs.SetFloat("floatPP", 1.2f);
PlayerPrefs.SetInt("integerPP", 10);
PlayerPrefs.SetString("stringPP", "UrhobA");
PlayerPrefs.DeleteAll(); // All the PlayerPrefs data we created was deleted.
DeleteKey("hash") - Deleting a Single PlayerPrefs Data
PlayerPrefs.DeleteKey("hash");
PlayerPrefs.SetFloat("floatPP", 1.2f);
PlayerPrefs.SetInt("integerPP", 10);
PlayerPrefs.SetString("stringPP", "UrhobA");
PlayerPrefs.DeleteKey("floatPP"); // The PlayerPrefs named "floatPP" was deleted.
PlayerPrefs.DeleteKey("stringPP"); // The PlayerPrefs named "stringPP" was deleted.Making Boolean Checks with PlayerPrefs
void Start()
{
PlayerPrefsSetBool("boolTest", false); // We save our PlayerPrefs as a bool.
bool state = PlayerPrefsGetBool("boolTest"); // We retrieve our PlayerPrefs as a bool.
Debug.Log(state.ToString()); // We print the retrieved data to the console.
}
bool PlayerPrefsGetBool(string hash) // We write our function for retrieving the data.
{
bool state = (PlayerPrefs.GetInt(hash) == 0) // With a ternary if, we retrieve the integer value, and if the value is 0, we return false; otherwise, not 0, true.
? false
: true;
return state;
}
void PlayerPrefsSetBool(string hash, bool value) // We write our function for saving the data.
{
int number = (value == false) // If the incoming data is false, we return 0; otherwise, 1.
? 0
: 1;
PlayerPrefs.SetInt(hash, number); // We add the numerical value we return as an Int PlayerPrefs.
}
Yes, friends, this is how you use PlayerPrefs. If you have any questions about PlayerPrefs, you can ask me in the comments section below.

Yorum Gönder