This class is used to specify preferences stored via the PlayerPrefInterface. It is derived from the PrefOptionBase class. It contains details about the current value of the option, and the Unity PlayerPref key-string of course, but it also contains a default value, and label GUIContent. It provides a Value accessor function, which, along with the constructor, handle loading and saving the value to PlayerPrefInterface for the programmer.
But keep in mind, the load/save operation will be performed every-time Value is accessed: while quite convenient, this could effect performance of loops and such.
- See also
- Sample usage: EditorPrefOptionSample.cs PlayerPrefTester.cs
- Template Parameters
-
TValueType | Defines the type of value that will be stored by this preference. Currently only Color, Vector2, Vector3, float, bool, string, int, Enum are valid types for an PlayerPrefOption ValueType. Using other, unsupported types, will generate runtime exceptions, but NOT compiler errors. To support additional types derive a new class from this one, and override the Has,Load,Save and Delete Functions. Be sure to call your new code first, returning upon success, to prevent an exception being generated. |
|
| XmlPrefOption (TValueType _defaultValue, string keyString, GUIContent guiLabel, bool _alwaysLoadAndSaveOnValueAccess=true) |
| Constructor takes almost all members as parameters, with the exception of the current value. The current preference value is set when loaded, if the key exists, or set to the default value, if new.
|
|
| PrefOptionBase (ValueType _defaultValue, string keyString, GUIContent guiLabel, bool _alwaysLoadAndSaveOnValueAccess=true) |
| Constructor takes almost all members as parameters, with the exception of the current value. The current preference value is set when loaded, if the key exists, or set to the default value, if new. It is up to the user or derived classes to call Load() when appropriate- the constructor will NOT automatically load the preference.
|
|
virtual void | Delete () |
| The Delete function does not destroy this runtime instance; it will set the runtime value member to it's default value, but first, It will attempt to remove the current key (and associated values) from stored Prefs. If the type ValueType of the PlayerPrefObject, is not supported NO exception is raised, and the entry with the given key, if it exists, is deleted.
|
|
virtual void | Load () |
| This function will attempt to retrieve from storage, and cache, the preference value from the PrefInterface, based on the current key value. If the key does not exist in PlayerPrefs, the defaultValue will be loaded instead. If the type ValueType of the PlayerPrefObject, is not supported an exception is raised.
|
|
virtual void | Save () |
| The Save function will attempt to store the current member _value into PrefInterface, using the current key value. If the type ValueType of the PlayerPrefObject, is not supported an exception is raised.
|
|
virtual void | SetToDefault () |
| Sets the current value of this preference to defaultValue.
|
|
|
static void | Delete (string keyParam) |
| The static function will delete all preferences of ValueType specified by the key.
|
|
static bool | HasKey (string keyParam) |
| Confirm the key is already stored in preferences
|
|
static object | Load (string keyParam) |
| This function will attempt to retrieve from storage, the preference value specified by the key-parameter, from the PrefInterface. If the key does not exist in PlayerPrefs, the defaultValue for the ValueType will be loaded instead.
If the type ValueType of the PlayerPrefObject, is not supported an exception is raised.
|
|
static ValueType | LoadAndCast (string key) |
| This function will attempt to retrieve from storage, and return, the preference value from the PrefInterface, based on the provided key value. If the key does not exist in PlayerPrefs, the defaultValue will be loaded and returned instead.
|
|
static void | Save (string keyParam, object _value) |
| A Primary Function of this class, this static version takes a key and an object, determines the appropriate value type of the object, and stores it in preferences. If the value type of the object is not supported, or does not match the ValueType of this class, an exception is raised.
|
|
virtual bool | HasKey () |
| This protected function is used to check if the key exits in PrefInterface. Used before attempting to load or delete keys. No type exceptions are generated.
|
|
bool | AlwaysLoadAndSaveOnValueAccess [get] |
| When true, loading and saving will happen every-time the value of this preference is accessed. This induces a performance hit, but makes the coding simpler. This value is assigned during construction and may not be changed after that.
|
|
string | Key [get] |
| The unique key used to reference this prefOption in storage.
|
|
GUIContent | Label [get, set] |
| The accessor allows one to read/write the optional label assigned to the preference options. May be null if no label is assigned, no checking is performed.
|
|
virtual ValueType | Value [get, set] |
| This Accessor's get function returns the current value of this preference. This current value is loaded, and cashed during initialization. If the current value object does not match the type ValueType, of the PlayerPrefOption, an exception is raised. If no value has been assigned yet, the defaultValue will, usually, be returned. However, if the type of the defaultValue object does not match the ValueType type of PlayerPrefOption, default(ValueType), will be returned instead. You can set defaultValue to null, if this is the desired result. The set functions will store the new value in the cache, but will NOT save it to PlayerPrefs (until Save(), is called). If the object to store does not match the type ValueType, of the PlayerPrefOption, an exception is raised. This accessor will check type compatibility before assigning or returning the preference's Value, and will either raise an exception or display a waning, respectively.
|
|