Player preferences - loading data
Having previously saved data with PlayerPrefs
through Set
functions, you can easily load that data back through the accompanying Get
functions (GetInt
, GetFloat
, and GetString
). With these, you simply specify a unique key name, and Unity returns the respective value. Consider the following code:
int HighScore = PlayerPrefs.GetInt ("HighScore", 0); float RemainingTime = PlayerPrefs.GetFloat ("RemainingTime", 0f); string Name = PlayerPrefs.GetString ("PlayerName", "Jon Doe");
Each Get
function requires only one essential parameter, namely, the key to retrieve. This is the first parameter, but what happens if you specify a key that doesn't exist? Which value should be returned, by default, in that case? To handle this, each Get
function supports a second, optional parameter, which is always returned if the specified key doesn't exist.
Note
You can also query whether a specified key exists using the HasKey
function--https://docs.unity3d.com/ScriptReference/PlayerPrefs...