OnValueChangedAttribute

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

namespace Sirenix.OdinInspector
{
[AttributeUsageAttribute((AttributeTargets)384, AllowMultiple = True, Inherited = True)]
[DontApplyToListElementsAttribute]
public sealed class OnValueChangedAttribute : Attribute
}

Requirements

Namespace: Sirenix.OdinInspector
Assembly: Sirenix.OdinInspector
Version: 1.0.6.1

Constructors

Definition Description
OnValueChangedAttribute(string, bool) Adds a callback for when the property's value is changed.

Properties

Return Type Definition Description
bool IncludeChildren Whether to invoke the method when a child value of the property is changed.
string MethodName Name of callback member 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;
		}
	}
}