Using PlayerPrefs in Unity

Saving and loading data are a continuous process in any program. But when you have to terminate the program and reload it, then you need to save the data to disk or web to retrieve it later. In game development you need to save data like how many levels were completed, player current level, pause and resume data. These data will be used when the player starts his next session. So solve this issue Unity has a PlayerPrefs class. In this tutorial, we will see how to save and retrieve data using PlayerPrefs.

What is PlayerPrefs?

PlayerPrefs is a class that stores and retrieves data from local storage between gameplay sessions. PlayerPrefs can save the following

  • Float
  • Int
  • String
Unity PlayerPrefs banner image

Where does PlayerPrefs save the data?

Operating SystemSave Location
Mac Os~/Library/Preferences/com.ExampleCompanyName.ExampleProductName.plist
WindowsHKCU\Software\ExampleCompanyName\ExampleProductName
Linux%userprofile%\AppData\Local\Packages\[ProductPackageId]\LocalState\playerprefs.dat
Windows Store App%userprofile%\AppData\Local\Packages\[ProductPackageId]\LocalState\playerprefs.dat
Windows Phonelocal folder
Android/data/data/pkg-name/shared_prefs/pkg-name.v2.playerprefs.xml
WebGlLocation depends on IndexedDB API

How to save Data using PlayerPrefs

PlayerPrefs uses a string to save the data. This string is called keyname. You need to specify separate keyname for each variable you need to save. “PlayerPrefs.Save()” is need for all save operation.

Save float


PlayerPrefs.SetFloat(KeyName, 2.0f);
PlayerPrefs.Save();

Save int

PlayerPrefs.SetInt(KeyName, 2); 
PlayerPrefs.Save();

Save String

PlayerPrefs.SetString(KeyName,"test");
PlayerPrefs.Save();

How to retrieve data using PlayerPrefs

To get the data back, you need only the keyname. Remember the keyname is case-sensitive, so you need to be very careful when typing the keyname.

Getfloat

float test = GetFloat("keyname");

GetInt

int test = GetInt("keyname");

Getstring

string test = GetString("keyname");

You can set a default value to get function as the second input. If the keyname doesn’t exist then the function will return the default value. Here is an example.

float test = GetFloat("keyname",1.0f);

Check it a keyname exists and delete it

You can check if a Keyname exists using the HasKey function . Let’s see an example on how to check if a keyname exists and delete it if it does.

void Keycheck(string Test)
    {
        if (PlayerPrefs.HasKey(Test))
        {
            DeleteKey(Test);
            Debug.Log("The key " + Test + "is deleted");

        }
        else
            Debug.Log("The key " + Test + " does not exist");
    }

Delete all keynames and values

This is generally used to reset all gamedata. You must use it with caution. It better to put a confirmation popup before exection the delete all command.

PlayerPrefs.DeleteAll();

This deletes are stored Keynames and values.

Uses of PlayerPrefs

PlayerPrefs can be used for a many purposes. Here are some major uses to note

Is PlayerPrefs Fast?

No, PlayerPrefs is not as fast as the load and saves in scripts. So, its not recommended to use PlayerPrefs for in-game computation. It is a good practice to load all your PlayerPrefs data during scene load.

Get all saved PlayerPrefs

It not possible directly through code to view all the saved PlayerPrefs but you can use the asset store plugin called PlayerPrefs ultimate to load all saved PlayerPrefs. This plugin also lets you save and load data visually. If you are someone who doesn’t like to code then this is the way to go.

PlayerPrefs alternative

PlayerPrefs are not safe to safe important data. In that case, its better so save your data in a server. It depends on what type of requirement your game has. If you need a lot of data processing then, its best to go with a database engine.

If you are still looking to save your data locally then I would suggest looking at Application persistent data path in Unity(binary) and file encryption to prevent data theft.

Binary vs PlayerPrefs

PlayerPrefs are best to store small amount of data. If you have a lot of data to store then best go with binary.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.