Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question Do we have events for +/- buttons for serialized list?

Discussion in 'Editor & General Support' started by MagicianArtemka, Mar 14, 2021.

  1. MagicianArtemka

    MagicianArtemka

    Joined:
    Jan 15, 2019
    Posts:
    46
    Hi guys.

    I just want to ask if Unity has any events when the user clicks these buttons? If not - maybe anybody knows the way how to hook up these actions?

    upload_2021-3-14_11-57-21.png
     
  2. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,304
    Could do it like this..
    Code (CSharp):
    1.  
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class TestListChangeEvent : MonoBehaviour
    5. {
    6.   private int previousCount = 0;
    7.   public List<int> ints = new List<int>();
    8.   private void OnValidate()
    9.   {
    10.     if (ints.Count != previousCount)
    11.     {
    12.       Debug.Log("change detected: " + ((ints.Count > previousCount) ? "more" : "less") + " items now.");
    13.       previousCount = ints.Count;
    14.     }
    15.   }
    16. }
    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.
     
    Last edited: Mar 14, 2021
    MagicianArtemka likes this.
  3. MagicianArtemka

    MagicianArtemka

    Joined:
    Jan 15, 2019
    Posts:
    46
    I love you! (like an old friend, not like I want to have sex with you) Thanks for the help
     
    adamgolden likes this.