Class PlayerPrefs
Provides a static interface for storing and retrieving simple player data to a local JSON file.
public static class PlayerPrefs
- Inheritance
-
PlayerPrefs
- Inherited Members
Remarks
Supported Types: int, double, bool, string.
Methods
DeleteAll()
Removes all keys and values from the preferences, then saves to disk.
public static void DeleteAll()
DeleteKey(string)
Removes the given key and its value from the preferences, then saves to disk.
public static void DeleteKey(string key)
Parameters
keystringThe key to remove.
Get<T>(string, T?)
Retrieves the value associated with the given key.
public static T? Get<T>(string key, T? defaultValue = default)
Parameters
keystringThe unique key to identify the preference.
defaultValueTThe value to return if the key does not exist.
Returns
- T
The stored value, or
defaultValueif not found.
Type Parameters
TThe type of the value.
HasKey(string)
Checks if the given key exists in the preferences.
public static bool HasKey(string key)
Parameters
keystringThe key to check.
Returns
- bool
trueif the key exists, otherwise,false.
Initialize()
Initializes the storage directory and loads existing preferences from disk.
public static void Initialize()
Remarks
File Locations:
- Windows:
%AppData%\MonoEight-Game\playerprefs.json - Unix/Mac:
~/.local/share/MonoEight-Game/playerprefs.json
Set<T>(string, T)
Sets the value for the given key and writes the changes to disk immediately.
public static void Set<T>(string key, T value)
Parameters
keystringThe unique key.
valueTThe value to store.
Type Parameters
TThe type of the value.