1
\$\begingroup\$

Does Unity fire any events when the user clicks these buttons? Or is there a way how to hook up code to respond to these actions?

enter image description here

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

The answer is here - we can use OnValidate event - https://forum.unity.com/threads/do-we-have-events-for-buttons-for-serialized-list.1074768/

using System.Collections.Generic;
using UnityEngine;
public class TestListChangeEvent : MonoBehaviour
{
  private int previousCount = 0;
  public List<int> ints = new List<int>();
  private void OnValidate()
  {
    if (ints.Count != previousCount)
    {
      Debug.Log("change detected: " + ((ints.Count > previousCount) ? "more" : "less") + " items now.");
      previousCount = ints.Count;
    }
  }
}

Edit: To avoid an initial event, could serialize previousCount, or default that to -1, and in OnValidate if it's -1 just set it to the current count and return instead of checking.

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .