Version 2.1.0.0

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)]
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 methodName, bool includeChildren = false)
Parameters
System.String methodName

Name of the method.

System.Boolean includeChildren

Whether to invoke the method when a child value of the property is changed.

Fields

IncludeChildren
Whether to invoke the method when a child value of the property is changed.
public bool IncludeChildren
MethodName
Name of callback member function.
public string MethodName

This website is brand new and built with the Odin 2.1 beta in mind. This means you might discover some features such as attribute expressions that are only available in the beta.

You can download the 2.1 beta from here and read the patch-notes here.