What is Unity PlayerPrefs? How Is It Used?

Unity PlayerPrefs Nedir? Nasıl Kullanılır?

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;

  1. Keeping the High Score count.
  2. Keeping the gold count.
  3. Retrieving data independently from the script when moving from one scene to another.
PlayerPrefs can be used in the examples I listed above; actually, it can be used for many other things as well. However, I recommend using it for simple operations. Because PlayerPrefs is not a normal database, it has its limits and may result in your game crashing with very large values.

With PlayerPrefs you can store and manage "Int", "Float", and "String" values.

How to Use PlayerPrefs?

Set Methods - Creating and Editing PlayerPrefs

We use the same method to create and edit a 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

We use this method to save our PlayerPrefs data in a controlled (safe) way.
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

So far we have learned to create, edit, and save data to disk; now let's look at how to retrieve this data we have created.
To retrieve data, we call our method as follows.
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

We have created all this data, and at later stages of the game, we may need to delete this data, so how do we do this? 
There are actually many ways to delete data. You can use the Set Methods to set values to 0, but this is not useful. For this situation, Unity provides us with 2 "Delete" methods.
  1. DeleteAll();
  2. DeleteKey("hash");
Let's examine how to use these.

DeleteAll() - Deleting All PlayerPrefs

This is the method we use to delete all PlayerPrefs data.
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

This is the method where we enter just the hash / key values of the data we want to delete and delete them.
PlayerPrefs.DeleteKey("hash");
This is how it is used; let's see how it is used in code.

        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

As you know, PlayerPrefs are used to store only 3 different data types and do not allow us to return a bool value. With a few small functions we will write, we can save bool data 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.