We all know that PlayerPrefs can at times be annoying to deal with directly. Problems include spelling keys differently in different places and inconsistent default values to name a few. Thus I've come up with a decent system for managing PlayerPrefs, and it works like this. You make a static class, called Prefs, and expose PlayerPrefs Get/Set methods via static properties. This class is the sole class that communicates with PlayerPrefs. All other classes go through Prefs in order to read/write from PlayerPrefs. It offers many advantages including: 1) Allows consistent default value for all prefs. 2) Eliminates the risk of misspelling a pref key. 3) Allows you to document the intent/purpose of each pref. 4) Allows you to easily increment an int for example via Prefs.LastScore += 10 5) Allows you to make a pref of any type (bool for example), by doing all the conversion inside of Prefs. Here's a sample code: Code (csharp): using UnityEngine; using System.Collections; public static class Prefs { // Static constructor useful for version checking static Prefs() { if (Version != "1.5") { // Invoke upgrade process if necessary. } } ///<summary> /// Gets the version. ///</summary> ///<value>Theversion.</value> public static string Version { get { returnPlayerPrefs.GetString("version", "1.0"); } } ///<summary> /// The score of the most recently played game. Insert more descriptive info here. ///</summary> public static int LastScore { get { return PlayerPrefs.GetInt("last score", 0); } set { PlayerPrefs.SetInt("last score", value); } } ///<summary> /// Prefs allows any type to become a pref key, and is not limited to the basic /// int, float, and string types. ///</summary> public static bool MusicEnabled { get { return PlayerPrefs.GetInt("music enabled", 1) != 0; } set { PlayerPrefs.SetInt("music enabled", value != 0); } } } Then the rest of your code does very little work to read, write, and modify PlayerPrefs. Code (csharp): void OnTriggerEnter() { Prefs.LastScore = 250; } void OnMusicButton() { Prefs.MusicEnabled = !Prefs.MusicEnabled; } I've shipped many games with this system and it's really works great.
Hello and thank you for this snippet. The set code for the bool should be set {PlayerPrefs.SetInt("music enabled", value? 1 : 0); } instead of set {PlayerPrefs.SetInt("music enabled", value!=0); } Or am I wrong?
Personally, I always change this... Code (csharp): public static int LastScore { get { return PlayerPrefs.GetInt("last score", 0); } set { PlayerPrefs.SetInt("last score", value); } } to this: Code (csharp): private const string s_LastScore = "last score"; public static int LastScore { get { return PlayerPrefs.GetInt(s_LastScore, 0); } set { PlayerPrefs.SetInt(s_LastScore, value); } } Otherwise you STILL have the "I mistyped the string one time" problem. You just moved it somewhere else. This eliminates it. (Wow, sorry, didn't notice this was a necro post... my bad)
Since it has been necro'd. I tend to save in to parts. The data class just serialise a regular class, and turn the resulting data into a string. The save class dumps the string into player prefs, or a server, or a file. This way the set up is pretty system agnostic. The data class doesn't care how the data is saved. The save class doesn't care how the data is used or generated.