OnValueChangedAttribute

OnValueChanged is used on any property, and calls the specified function whenever the value of the property is changed.

Use this to do custom logic whenever the property is changed.

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.4.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;
		}
	}
}