Unity Feature 101: Basic Saving Using PlayerPrefs
In this article, we will explore the PlayerPrefs Class in Unity. PlayerPrefs is the simplest form of saving persistent data in Unity. In other words, if want save something in Unity and quit the project and then play again, you will have your saved data loaded into the project again.
Overview
PlayerPrefs class is the simplest way of saving persistent data in Unity. As a general rule, they should only be used to save simple data like some strings (ie. player name) or floats or integers and nothing too complex. It is also recommended that you use them for your prototypes or very simple games.
How To
The main three methods in PlayerPrefs are “Set”, “Get”, and “Save”.
The Set and Get methods work like dictionaries, they accept two parameters: a key — which are strings, and a value. The PlayerPrefs accept only three value types: floats, integers, or strings. Let us see it in action…
Now at the start of the game, Unity will save a key named “level” that has a value of 2. To retrieve it we use the method “Get”.
Now if you check your console, it should read “2”.
Please do note the following:
- The keys are case sensitive. So if you set a key “level” and then try to get a key “levEl”, you will get the default value of the key. So if it is a float or an int, you will get zero.
- PlayerPrefs.Get(int,float, or string) has a 2nd parameter called “default” value. If the system checks and find our that this key has not been set before, then it will give it the default value that you have set for it. In our example above, we can set a default value of 5 for example and then the console reads 5 if it has not been set before.
3. PlayerPrefs are only saved to the disk only when the game stops playing. So for example you are running your game and for any reason you do not quit the game properly, then all the PlayerPrefs would not have been saved properly.
In order to avoid this, PlayerPrefs has a handy method called “Save”. PlayerPrefs.Save(); does exactly what you think. It will save the PlayerPrefs to the disk without the need to quit the game.
4. If you are wondering where is the PlayerPrefs data saved, well it depends on what platform you are working on. You can check Unity’s Scripting API to see exactly the path for each platform.
Storing a bool using PlayerPrefs
As you saw, PlayerPrefs do not accept bools as values but you can do it. First you would create a method to set the bool based on int value. So if value is 1 then true but if 0 then the bool is false. Secondly, you create a method that returns a true bool if the PlayerPrefs.GetInt is equals to 1.
Adding Complexity
You can store more even more data using PlayerPrefs if you combine it with JSON. Without getting into the details of what JSON is, JSON is a way of encoding all kinds of data into simple strings. Let’s see how we can do this thru code:
A. Saving to Json
If done correctly, then the above will save a Json file using PlayerPrefs with a key called “SavedPlayer”. Now we can use this key to get all the saved data of the player.
B. Loading FromJson
In order to load the above data, we will use JsonUtility.FromJson which will return an object of the custom class we made. This way we can use this object to use all the data we saved.
Check your console and you will see that all the data that has been saved is now loaded correctly.
PlayerPrefs is a very useful class and a simple way to save persistent data. As you saw in this article it can be manipulated to save bools and combined with Json to save complex data. Yet it is also recommended to only use PlayerPrefs for your prototypes or saving simple data in your game or even using it as a save system for your small and simple projects.