PrefOptionBase's are used by the programmer to completely specify a "Preference setting" in a standardized manner. Both, the type of value stored, and a concrete type implementing IStorePreferences, are used as generic arguments for this class. When the PrefOption does NOT need to be PrefInterfaceType agnostic: Both EditorPrefOption and PlayerPrefOption classes are derived from this one, with each using a specific IStorePreferences interface for the PrefInterfaceType parameter. Unless creating your own IStorePreferences implementing class, users should usually implement one of those two derived classes, rather than this one. Members contains details about the current value of the option, and the key string which is used to uniquely identify the preference. It also contains a default value, and a display label GUIContent. It provides a Value accessor function, which, does NOT do automatic loading and saving the value to PlayerPrefs for the programmer (thus "manual" in the class name). If you want Load and Save to be automatic, use the derived PlayerPrefOption or EditorPrefOption classes instead.
- Template Parameters
-
ValueType | Define the type of value. Currently only Color, Vector2, Vector3, float, bool, string, int, Enum, Type, and Unity.Object are valid types for a PrefOptionBase ValueType. Using other, unsupported types, will generate runtime exceptions, but NOT compiler errors. To support additional types derive a new class from PrefOptionBase, and override the HasKey ,Load ,Save and Delete Functions. Be sure to call your new code first, returning upon success, to prevent an exception being generated. |
PrefInterfaceType | This option specifies which interface should be used to store the preferences. This module derives classes from PrefOptionBase that use PlayerPrefInterface, and EditorPrefInterface for this type parameter. |
|
| 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.
|
|
bool | AlwaysLoadAndSaveOnValueAccess [get] |
| specifies weather using the Value accessor will automatically load or save the value to the preference storage.
|
|
GUIContent | Label [get, set] |
| Accessor to get and set the label assigned to the preference
|
|
ValueType | Value [get, set] |
| Accessor to get and set the value assigned to the preference
|
|