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.
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?
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.
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.