Skip to content

ScriptableValue<T> Class

A ScriptableValue<T> that holds a value of type T.

C#
public abstract class ScriptableValue<T> : ScriptableValue, INotifyPropertyChanging, INotifyPropertyChanged, IStackTraceProvider, ICanBeReadOnly

T
The type of the value.

Inheritance object → UnityEngine.Object → UnityEngine.ScriptableObject → RuntimeScriptableObjectScriptableValue

Implements INotifyPropertyChanging, INotifyPropertyChanged, IStackTraceProvider, ICanBeReadOnly

The current value. This can be changed at runtime.

C#
[CreateProperty]
public T Value { get; set; }

The previous value before the current value was set.

C#
[CreateProperty]
public T PreviousValue { get; }

The default value. This is used when the value is reset.

C#
[CreateProperty]
public T DefaultValue { get; set; }

Called before the value is set. This can be used to prevent the value from being set.

C#
protected virtual bool OnBeforeSetValue(T oldValue, T newValue)

oldValue T
The current value.

newValue T
The new value that is going to be set.

bool
true if the value can be set; otherwise, false.

This is called before the OnValueChanging event. This is also called no matter what notify is set as.

Called when the value is set. This can be used to do something when the value is set.

C#
protected virtual void OnAfterSetValue(T oldValue, T newValue)

oldValue T
The old value that was set.

newValue T
The new value that was set.

This is called before the OnValueChanged event. This is also called no matter what notify is set as.

Helper method to check if the new value is the same as the old value.

C#
protected bool IsSameValue(T newValue, T oldValue)

newValue T
Thew new value.

oldValue T
The old value.

bool
true if the values are the same; otherwise, false.

Sets the current value without invoking the OnValueChanging and OnValueChanged events.

C#
public void SetValueWithoutNotify(T newValue)

newValue T
The new value.

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.

C#
protected override void OnPreStart()

If this isn’t called when the game starts, it’s called once this scriptable object is loaded and instantiated.

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.

C#
protected override void OnDisabled()

This can also be called during the game is the scriptable object is destroyed and/or unloaded.

Resets the value to the default value.

C#
public void ResetValue()

Warns if there are any left-over subscribers to the events.

C#
protected override void WarnIfLeftOverSubscribers()

This will only be called in the Unity editor and builds with the DEBUG flag.

Removes any subscribers from the event.

C#
public void ClearSubscribers(bool warnIfLeftOver = false)

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.

C#
protected virtual void OnValidate()

Called before the current value is set.

C#
public event ValueEventHandler<T>? OnValueChanging

ValueEventHandler<T>

Called after the current value is set.

C#
public event ValueEventHandler<T>? OnValueChanged

ValueEventHandler<T>