× Phaseshift FCOM Tutorials

Storing data with PlayerPrefs

Learn how to store player data using Unity's PlayerPrefs utility

PlayerPrefs is a Unity utility that allows you to store data via local files on the player's computer. This can be especially useful when storing information regarding the game settings when running your game, such as graphics, audio or input data. It can also be used to store player save data, like character progress, level, credits, inventory, you name it.


Though you can store player related save data; it is not secure. Players can access their data stored on their local file and tweak settings. If you have sensitive data such as progression flags, level/experience or currency data that you do not want the player to have access to, it is recommended you encrypt the data, or use another utility. For most use cases, PlayerPrefs is fine.

Basic data storage

PlayerPrefs can only store data in the following data types: int, string, or float. This means you cannot store bool, arrays or objects...in the traditional way. You CAN however, convert your objects into a data type that PlayerPrefs can store. We will be discussing how do that later.

For now though, let's keep it simple. The below shows all of the methods you can use to store all of the available data types.

When you need to retrieve this data, you can get it in a similar way: 

And that's it! The data you store in this way can be accessible between game play sessions using the methods above. 

Storing more complex data types

What if say, you want to store something more complex like an object you built to manage inventory or an upgrade tracking object? How do you go about storing that using PlayerPrefs

This is where JSON comes in. JSON (or JavaScript Object Notation) is a standard text-based format for representing structured data based on JavaScript object syntax. In simple terms, JSON is classified as a string, meaning PlayerPrefs can store the data. We can convert your C# object into a JSON encoded string then store it. You can then decode the string back into the object it was converted into. 

Let's look at an example. We have here a C# class that has all kinds of variables we want to store. We have a string, a Color, an int, a float, and a List featuring a Custom built class. We can store all of these using JSON and PlayerPrefs.

To save this data as JSON, we can in the same class, create a method that calls upon JSONUtility.ToJson() to convert this object into a string. We can then store the string with PlayerPrefs. For the below code snippet to work, you need to make sure that the method exists inside the class you want to save.

We can then retrieve this data much the same way, we need to GetString("PlayerSaveData") then re-convert the JSON into our PlayerSaveData object.


If you want to keep your save and load data completely self-contained, you could add the script to a GameObject that doesn't DestroyOnLoad. You can then call Load() on Awake(), Start() or OnEnable() (depending on your use case) and your Save() method could get called on your OnDisable() or whenever your game closes. It's ALWAYS good practice to save your data whenever something important has changed with the save data, such as completing a level, or gaining XP. That way if the game crashes or your player leaves the game, their data is always saved regardless of how they've left your game.

Accessing locally stored PlayerPrefs data

Whenever you need to access or delete locally stored PlayerPrefs data, you need to be aware of platform specific directories that Unity will use when saving. Ripped completely from their documentation, here is a list of platform specific directories where you can access the saved data.

Standalone player and in-Editor Play mode storage location

- MacOS: ~/Library/Preferences/com.ExampleCompanyName.ExampleProductName.plist

Standalone Player storage location

- Android: /data/data/pkg-name/shared_prefs/pkg-name.v2.playerprefs.xml. Note that C#, Android Java and native code can all access the PlayerPrefs data.

- iOS: Uses the [NSUserDefaults standardUserDefaults] API to store PlayerPrefs data.

- Linux: ~/.config/unity3d/ExampleCompanyName/ExampleProductName

- WebGL: Unity stores up to 1MB of PlayerPrefs data using the browser's IndexedDB API. For more information, see IndexedDB.

- Windows: HKCU\Software\ExampleCompanyName\ExampleProductName

- Windows Universal Platform: %userprofile%\AppData\Local\Packages\[ProductPackageId]\LocalState\playerprefs.dat

In-Editor Play mode storage location

- Windows: HKCU\Software\Unity\UnityEditor\ExampleCompanyName\ExampleProductName key. Note that Windows 10 uses the application’s PlayerPrefs names. For example, Unity adds a DeckBase string and converts it into DeckBase_h3232628825. The application ignores the extension.

Full example


To explore more on these topics, check out the documentation:


Liked this article?

Please consider sharing!

Josh Lyell

Game Developer