Hide
OnValueChanged

Call a function every time the field value is changed

  • string callback the callback function name

    It'll try to pass the new value and the index (only if it's in an array/list). You can set the corresponding parameter in your callback if you want to receive them.

  • Allow Multiple: Yes

Special Note: AnimatorState will have a different OnValueChanged parameter passed in. See AnimatorState for more detail.

Known Issue:

Unity changed how the TrackPropertyValue and RegisterValueChangeCallback works. Using on a SerializeReference, you can still get the correct callback, but the callback will happen multiple times for one change.

Using OnValueChanged on an array/list of SerializeReference can cause some problem when you add/remove an element: the Console will give error, and the inspector view will display incorrect data. Selecting out then selecting back will fix this issue. However, you can just switch back to the old way if you do not care about the field change in the reference field. (Because Unity, still, does not fix related issues about property tracking...)

These two issues can not be fixed unless Unity fixes it.

See: 1, 2

using SaintsField; // no params [OnValueChanged(nameof(Changed))] public int value; private void Changed() { Debug.Log(
quot;changed=
{value}"); } // with params to get the new value [OnValueChanged(nameof(ChangedAnyType))] public GameObject go; // it will pass the index too if it's inside an array/list [OnValueChanged(nameof(ChangedAnyType))] public SpriteRenderer[] srs; // it's ok to set it as the super class private void ChangedAnyType(object anyObj, int index=-1) { Debug.Log(
quot;changed=
{anyObj}@{index}"); }

You can use static method too (see the syntax in the end of the document)

// Call `Debug.Log(oj)` on changed [OnValueChanged(":Debug.Log")] public Object oj;

List/Array Change

Important
Enable SaintsEditor before using this feature

If you have SaintsEditor enabled, OnValueChanged can response to element add/remove.

For add element, it uses the same signature.

For remove element, it uses

MyCallback(MyType arrayOrListType, int negativeCount);
  • MyType arrayOrListType: the array/list field. Need to be the same as you announced.
  • negativeCount: a negative number to present how many elements has been removed.
// Enable `SaintsEditor` before trying this example using SaintsField; [OnValueChanged(nameof(OnArrayChanged))] public string[] arrayChanged; private void OnArrayChanged(string content, int index) { Debug.Log(
quot;array[
{index}]={content}"); } private void OnArrayChanged(string[] arrayItself, int removedCount) { Debug.Log(
quot;array.length removed
{-removedCount}, current length={arrayItself.Length}"); }

Output:

// Click the `+` button
array[1]=Something
// Click the `+` button
array[2]=Something
// Input `1` in the size input in array/list drawer
array.length removed 2, current length=1
// Input `5` in the size input in array/list drawer
array[1]=Something
array[2]=Something
array[3]=Something
array[4]=Something
// Click the `-` button
array.length removed 1, current length=4