Version 3.3.1.2

Odin has a dedicated attribute overview with examples

OnValueChangedAttribute class

Namespace: Sirenix.OdinInspector
Assembly: Sirenix.OdinInspector.Attributes
[DontApplyToListElements]
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Module | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Interface | AttributeTargets.Parameter | AttributeTargets.Delegate | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter | AttributeTargets.All, AllowMultiple = true, Inherited = true)]
[Conditional("UNITY_EDITOR")]
public sealed class OnValueChangedAttribute : Attribute, _Attribute

OnValueChanged works on properties and fields, and calls the specified function whenever the value has been changed via the inspector.

Inheritance
  • System.Object
  • System.Attribute
  • OnValueChangedAttribute
Remarks

note

Note that this attribute only works in the editor! Properties changed by script will not call the function.

Example

The following example shows how OnValueChanged is used to provide a callback for a property.

public class MyComponent : MonoBehaviour
{
	[OnValueChanged("MyCallback")]
	public int MyInt;

	private void MyCallback()
	{
		// ..
	}
}
Example

The following example show how OnValueChanged can be used to get a component from a prefab property.

public class MyComponent : MonoBehaviour
{
	[OnValueChanged("OnPrefabChange")]
	public GameObject MyPrefab;

	// RigidBody component of MyPrefab.
	[SerializeField, HideInInspector]
	private RigidBody myPrefabRigidbody;

	private void OnPrefabChange()
	{
		if(MyPrefab != null)
		{
			myPrefabRigidbody = MyPrefab.GetComponent<Rigidbody>();
		}
		else
		{
			myPrefabRigidbody = null;
		}
	}
}

Constructors

OnValueChangedAttribute(String, Boolean)
Adds a callback for when the property's value is changed.
public OnValueChangedAttribute(string action, bool includeChildren = false)
Parameters
System.String action

A resolved string that defines the action to perform when the value is changed, such as an expression or method invocation.

System.Boolean includeChildren

Whether to perform the action when a child value of the property is changed.

Fields

Action
A resolved string that defines the action to perform when the value is changed, such as an expression or method invocation.
public string Action
IncludeChildren
Whether to perform the action when a child value of the property is changed.
public bool IncludeChildren
InvokeOnInitialize
Whether to perform the action when the property is initialized. This will generally happen when the property is first viewed/queried (IE when the inspector is first opened, or when its containing foldout is first expanded, etc), and whenever its type or a parent type changes, or it is otherwise forced to rebuild.
public bool InvokeOnInitialize
InvokeOnUndoRedo
Whether to perform the action when an undo or redo event occurs via UnityEditor.Undo.undoRedoPerformed. True by default.
public bool InvokeOnUndoRedo