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.
Note that this attribute only works in the editor! Properties changed by script will not call the function.
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()
{
// ..
}
}
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;
}
}
}
public OnValueChangedAttribute(string action, bool includeChildren = false)
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. |
public string Action
public bool IncludeChildren
public bool InvokeOnInitialize
public bool InvokeOnUndoRedo