Introduction
PlayerPrefs is a saving system that can be useful if you want to save a small number value. It works perfectly for int, float and string values. How do you actually introduce it in your project?
First of all, you need to add a canvas that will have a Text object, along with a Button. The text object will be a value, by default it will be set to 0. The button will be interactable and will increase or decrease the value. There will also be two buttons, that will be used for saving and loading. When you create those two elements, you need to create a C# script. Just add it in the Canvas object and name it SaveSystem. Here is the video that contains a detailed explanation:
if(PlayerPrefs.HasKey("ScoreValue"))
this is an if statement that checks if there is a key named ScoreValue. to make playerprefs system do anything to the project, you need to assign it to the score value. The score value will be called in a PlayerPrefs.GetInt, which is the method that will be used for loading the value from the saving system.
GetInt and SetInt methods
now you need to add public voids for the buttons. The first one you need is addPoints, which will only contain score += 1, the next one is savePoints, which will contain this line: PlayerPrefs.SetInt(“ScoreValue”, score). The last one is loadPoints, which will contain score = playerprefs.getint(scorevalue).

Saving and Loading
You need to add public voids for the buttons. The first one you need is addPoints, which will only contain score += 1, the next one is savePoints, which will contain this line: playerprefs.setint(scorevalue, score), and the last one is loadPoints, which will contain score = playerprefs.getint(scorevalue).
When you try to press the first button, it adds the score. You can save the value by clicking the save button, but if you increase the value even further and press load button, it will load the value that has been saved.
You can also add one more button: to reduce points, which is just for testing some values in Unity. Go to the script and add another public void, it will be called removePoints() and will have score -= 1 and here is what will happen: when you add a new button and instead of + you put a – sign, when you assign the removePoints() and try to press it, the value will be decreased.
Other potential for saving
This saving system has three ways of saving data: Save, which is used for saving all preferences. SetFloat is used for saving a float value. SetInt is used for saving an integer value and SetString, for saving a single string value.
PlayerPrefs, on the other hand, has many other ways to communicate with data in Unity. It is very practical for a project that keeps a small amount of data, such as current level, highscore and lives count for your player. It is also useful to save game settings such as music volume. PlayerPrefs has some additional features such as DeleteKey, which deletes a specific key from a project until it is created again, DeleteAll for deleting all keys and values that exist in a project.
Where is the data stored?
There is no file that can be saved in the Assets folder of your Unity project, so it has various ways of storing data, which can vary depending on the operating system:
MacOS: ~/Library/Preferences/com.ExampleCompanyName.ExampleProductName.plist
Android: /data/data/pkg-name/shared_prefs/pkg-name.v2.playerprefs.xml
Windows: HKCU\Software\ExampleCompanyName\ExampleProductName
Linux: ~/.config/unity3d/ExampleCompanyName/ExampleProductName
iOS: [NSUserDefaults standardUserDefaults]
WebGL stores data in a file that does not exceed 1MB of size, it uses IndexedDB API in the browser. That’s not a very good thing for saving data in larger files. Instead of PlayerPrefs, larger projects would most likely use JSON saving system because of its compatibility.


Why is JSON a better option?
JSON is a better option due to its ability to save larger amounts of data. It also offers better performance, so this video has a more detailed explanation.
No responses yet