11.2 PlayerPrefs Readings and Homework
PlayerPrefs (short for preferences) is a key value pair object that is serialized to a flat file on the user’s harddrive. The goal of this file is to allow data to persist between sessions of a game.
Some people pass data between scenes using PlayerPrefs, this means a read and write operation to the hard disk and it means this data could be corrupted either accidentally or overwritten purposefully, to cheat. It is recommended that you not put sensitive information in PlayerPrefs or that you encrypt it if you must.
For instance, in 11.1, if we had a high score ranking feature that uploaded the score to the internet, and if we saved the user score from MainScene to a file before loading it in ScoreScene, the user could have edited the score, cheating and uploading an implausibly high score to the internet.
Here is a use case tutorial using playerprefs to pass data. He mentions you could also cheat by editing experience points or powerup unlocks.
https://medium.com/@zachcaceres/persistent-data-between-scenes-how-to-use-playerprefs-in-unity-b6fd409363c3
In the Unity Documentation, you can see where the file is always stored. For some phones it would be difficult to access this file, but it is never impossible. You caution when storing data here no matter the platform. This documentation also gives an overview of PlayerPrefs access methods.
https://docs.unity3d.com/ScriptReference/PlayerPrefs.html
Here is a simple example of storing a String in Unity using PlayerPrefs
https://docs.unity3d.com/ScriptReference/PlayerPrefs.SetString.html
and another about accessing that String
https://docs.unity3d.com/ScriptReference/PlayerPrefs.GetString.html
You will need to research UI InputFields in order to do your homework
Homework Due Mon. April 20 before 10PM via github.
Create a new project called 11-2-your initials-PlayerPrefs.
Place a UIText element and UI InputField in a Canvas.
Your game should check playerprefs to see if there is already a value under the key “Name”.
If no name is saved, the UIText shows “Please Enter Your Name” *
If a name is saved, the UIText shows “Hello, Bob” (or whatever the user’s name is)
If the user types a new name in the InputField, update the PlayerPrefs.
*If you mess up or need to test the no saved name condition, you will have to research the code to delete a preferences (given in the links above). However, you do not need to have any delete buttons. If you wanted to be fancy, you could have an empty name entered in InputField delete the preference