Here’s a quick tutorial in storing persistent information about your Unity Games.
PlayerPrefs is a class in UnityEngine that stores and accesses player preferences between game sessions.
- The class works like a dictionary by pairing keys and values
- There is no boolean variable type, so use an int with 0 = false and 1 = true (old school)
- The web player only stores 1 Megabyte, keep that in mind and always use a try catch block to avoid uncaught exceptions
- This class will throw a PlayerPrefsException
//Save and retrieve a high score stored as an integer:
GameManager.instance.highScore = PlayerPrefs.GetInt("HighScore");
//Save and retrieve the players name:
GameManager.instance.highScore = PlayerPrefs.GetString("player1Name");
//Clear just the stored high score
//Clear all the data:
//Check to see if we've previously stored something, like the
//last saved level. If the key exists then load the level, if
//it does not, then load the first level.
The following is from the Unity Script Reference on exactly where the data is stored:
On Mac OS X PlayerPrefs are stored in ~/Library/Preferences folder, in a file named unity.[company name].[product name].plist, where company and product names are the names set up in Project Settings. The same .plist file is used for both Projects run in the Editor and standalone players.
On Windows, PlayerPrefs are stored in the registry under HKCU\Software\[company name]\[product name] key, where company and product names are the names set up in Project Settings.
On Linux, PlayerPrefs can be found in ~/.config/unity3d/[CompanyName]/[ProductName] again using the company and product names specified in the Project Settings.
On Windows Store Apps, Player Prefs can be found in %userprofile%\AppData\Local\Packages\[ProductPackageId]>\LocalState\playerprefs.dat
On Windows Phone 8, Player Prefs can be found in application’s local folder, See Also: Directory.localFolder
On Web players, PlayerPrefs are stored in binary files in the following locations:
Mac OS X: ~/Library/Preferences/Unity/WebPlayerPrefs