Tutorials, Unity

Storing Data in Unity – PlayerPrefs

Wajahat Karim  

This tutorial is the second part of Storing Data in Unity series. You can read Part 1 of this series here.

What’s in this part?

We will discuss about storing micro data such as int, float, string, and double in Unity using PlayerPrefs.

What we will develop?

Following is the preview of what we will be developing in this part.

DEMO DEMO DEMO DEMO

Where can I get the Source Code?

You can get the source code of this project here.

What are PlayerPrefs?

PlayerPrefs is a Unity class which stores and accesses player preferences between game sessions. The player preferences can be score of player, how many levels have been completed, how much stars has been earned by player, what is player’s name, and so on.

Where are PlayerPrefs stored in Unity?

Editor/Standalone

On Mac OS X PlayerPrefs are stored in ~/Library/Preferences folder, in a file named unity.[company name].[product name].plist, where company and product names are the names set up in Project Settings. The same .plist file is used for both Projects run in the Editor and standalone players.

On Windows, PlayerPrefs are stored in the registry under HKCU\Software[company name][product name] key, where company and product names are the names set up in Project Settings.

On Linux, PlayerPrefs can be found in ~/.config/unity3d/[CompanyName]/[ProductName] again using the company and product names specified in the Project Settings.

On Windows Store Apps, Player Prefs can be found in %userprofile%\AppData\Local\Packages[ProductPackageId]>\LocalState\playerprefs.dat

On Windows Phone 8, Player Prefs can be found in application’s local folder.

WebPlayer

On Web players, PlayerPrefs are stored in binary files in the following locations:

Mac OS X: ~/Library/Preferences/Unity/WebPlayerPrefs

Windows: %APPDATA%\Unity\WebPlayerPrefs

There is one preference file per Web player URL and the file size is limited to 1 megabyte. If this limit is exceeded, SetInt, SetFloat and SetString will not store the value and throw a PlayerPrefsException.

Writing data in PlayerPrefs

PlayerPrefs support 4 types of data: int, float, double, and string. To write data, we have to call static functions SetInt, SetFloat, and SetString to write int, float, and string respectively. These functions take two parameters: key which is a string and used as unique key of data and the value parameter.

For example, to store the score of player, we can call like this:

int myScore = 10;
PlayerPrefs.SetInt("score", myScore);

Reading data in PlayerPrefs

Like writing, PlayerPrefs support reading 4 types of data: int, float, double, and string. To read data, we have to call static functions GetInt, GetFloat, and GetString to read int, float, and string respectively. These functions take single string parameter ‘key’ to read the data with the key.

For example, to read the score of player from PlayerPrefs, we can call like this:

int myScore = 0;
myScore = PlayerPrefs.GetInt("score");

It is a good practice to always check the key for existence before reading data. Following code shows score reading with key existence check:

int myScore = 0;
if (PlayerPrefs.HasKey("score") == true)
    myScore = PlayerPrefs.GetInt("score");

** **

Thanks for reading it. In the next part, we will discuss about reading and writing data in Files using Unity.

    Tutorials, Unity