Skip to main content

PlayerPreferences

A static class in Alta.IO, the global namespace.

The game's replacement for Unity's PlayerPrefs: a typed key/value store kept in player.prefs in the storage folder, AES-encrypted with a key from the device id. Loaded on first use; changes stay in memory until SaveToDisk, which runs on quit.

Fields​

4 private and internal fields
MemberWhat it is
private const string FileNameName of the prefs file, player.prefs.
private static bool isChangedFlagTrue when values changed since the last save.
private static readonly Logger loggerNLog logger for this class.
private static Dictionary<string, object> valuesThe loaded values by key. Null until first use.

Properties​

1 private and internal property
MemberWhat it is
private static string SavePath { get; set; }Full path of player.prefs in AltaIO.StorageFolder.

Methods​

MemberWhat it is
static void DeleteAll()Clears all values and deletes the file. Doesn't load first, so it throws if nothing has read the prefs yet.
static bool GetBool(string identifier, bool defaultValue = false)Reads a bool, or the default if the key isn't set.
static float GetFloat(string identifier, float defaultValue = 0)Reads a float, or the default if the key isn't set.
static int GetInt(string identifier, int defaultValue = 0)Reads an int, or the default if the key isn't set.
static string GetString(string identifier, string defaultValue = "")Reads a string, or the default (empty) if the key isn't set.
static bool HasKey(string keyName)True if the key is set.
static void RemoveKey(string keyName)Removes a key if it's set.
static void SaveToDisk()Writes all values to the file, encrypted, if anything changed since the last save.
static void SetBool(string identifier, bool value)Sets a bool value in memory.
static void SetFloat(string identifier, float value)Sets a float value in memory.
static void SetInt(string identifier, int value)Sets an int value in memory.
static void SetString(string identifier, string value)Sets a string value in memory.
5 private and internal methods
MemberWhat it is
private static byte[] Decrypt(byte[] input)Decrypts the prefs file with AES using a key derived from the device's unique id, so the file can't be read on another machine.
private static byte[] Encrypt(byte[] input)Encrypts the prefs data with AES using a key derived from the device's unique id.
private static void EnsureMap()Loads the file the first time any value is read or written.
private static T GetValue<T>(string identifier, T defaultValue)Reads a value cast to the asked type. Throws if the key holds a different type.
private static void LoadFromDisk()Reads and decrypts the file into memory. Starts empty if the file is missing or can't be decrypted.