ScriptableValue<T> Class
Definition
Section titled “Definition”A ScriptableValue<T> that holds a value of type T.
public abstract class ScriptableValue<T> : ScriptableValue, INotifyPropertyChanging, INotifyPropertyChanged, IStackTraceProvider, ICanBeReadOnlyType Parameters
Section titled “Type Parameters”T
The type of the value.
Inheritance object → UnityEngine.Object → UnityEngine.ScriptableObject → RuntimeScriptableObject → ScriptableValue
Implements INotifyPropertyChanging, INotifyPropertyChanged, IStackTraceProvider, ICanBeReadOnly
Properties
Section titled “Properties”The current value. This can be changed at runtime.
[CreateProperty]public T Value { get; set; }PreviousValue
Section titled “PreviousValue”The previous value before the current value was set.
[CreateProperty]public T PreviousValue { get; }DefaultValue
Section titled “DefaultValue”The default value. This is used when the value is reset.
[CreateProperty]public T DefaultValue { get; set; }Methods
Section titled “Methods”OnBeforeSetValue(T, T)
Section titled “OnBeforeSetValue(T, T)”Called before the value is set. This can be used to prevent the value from being set.
protected virtual bool OnBeforeSetValue(T oldValue, T newValue)Parameters
Section titled “Parameters”oldValue T
The current value.
newValue T
The new value that is going to be set.
Returns
Section titled “Returns”bool
true if the value can be set; otherwise, false.
Remarks
Section titled “Remarks”This is called before the OnValueChanging event. This is also called no matter what notify is set as.
OnAfterSetValue(T, T)
Section titled “OnAfterSetValue(T, T)”Called when the value is set. This can be used to do something when the value is set.
protected virtual void OnAfterSetValue(T oldValue, T newValue)Parameters
Section titled “Parameters”oldValue T
The old value that was set.
newValue T
The new value that was set.
Remarks
Section titled “Remarks”This is called before the OnValueChanged event. This is also called no matter what notify is set as.
IsSameValue(T, T)
Section titled “IsSameValue(T, T)”Helper method to check if the new value is the same as the old value.
protected bool IsSameValue(T newValue, T oldValue)Parameters
Section titled “Parameters”newValue T
Thew new value.
oldValue T
The old value.
Returns
Section titled “Returns”bool
true if the values are the same; otherwise, false.
SetValueWithoutNotify(T)
Section titled “SetValueWithoutNotify(T)”Sets the current value without invoking the OnValueChanging and OnValueChanged events.
public void SetValueWithoutNotify(T newValue)Parameters
Section titled “Parameters”newValue T
The new value.
OnPreStart()
Section titled “OnPreStart()”Called when the game starts, before OnStart().
You should not subscribe to events in this method! Use OnStart() instead. OnPreStart() should be used for preparing the scriptable object.
protected override void OnPreStart()Remarks
Section titled “Remarks”If this isn’t called when the game starts, it’s called once this scriptable object is loaded and instantiated.
OnDisabled()
Section titled “OnDisabled()”Called when the game is ending, after OnPreDisabled().
You should not unsubscribe from events in this method! Use OnPreDisabled() instead. OnDisabled() should be used for cleaning up the scriptable object.
protected override void OnDisabled()Remarks
Section titled “Remarks”This can also be called during the game is the scriptable object is destroyed and/or unloaded.
ResetValue()
Section titled “ResetValue()”Resets the value to the default value.
public void ResetValue()WarnIfLeftOverSubscribers()
Section titled “WarnIfLeftOverSubscribers()”Warns if there are any left-over subscribers to the events.
protected override void WarnIfLeftOverSubscribers()Remarks
Section titled “Remarks”This will only be called in the Unity editor and builds with the DEBUG flag.
ClearSubscribers(bool)
Section titled “ClearSubscribers(bool)”Removes any subscribers from the event.
public void ClearSubscribers(bool warnIfLeftOver = false)Parameters
Section titled “Parameters”warnIfLeftOver bool
If true, a warning will be printed in the console if there are any subscribers.
The warning will only be printed in the editor and debug builds.
OnValidate()
Section titled “OnValidate()”protected virtual void OnValidate()Events
Section titled “Events”OnValueChanging
Section titled “OnValueChanging”Called before the current value is set.
public event ValueEventHandler<T>? OnValueChangingEvent Type
Section titled “Event Type”OnValueChanged
Section titled “OnValueChanged”Called after the current value is set.
public event ValueEventHandler<T>? OnValueChanged