mbonatsos
3/10/2019 - 10:57 AM

UnityEventTracked.cs

using UnityEngine.Events;

public class UnityEventTracked : UnityEvent
{
    private int _nonPersistentListenersCount = 0;

    /// <summary>
    /// Get the number of registered non persistent listeners.
    /// </summary>
    /// <returns></returns>
    public int GetNonPersistentEventCount() => _nonPersistentListenersCount;

    /// <summary>
    ///  Add a non persistent listener to the UnityEvent.
    /// </summary>
    /// <param name="action">Callback function</param>
    public new void AddListener(UnityAction action)
    {
        base.AddListener(action);
        _nonPersistentListenersCount++;
    }

    /// <summary>
    /// Remove a non persistent listener from the UnityEvent.
    /// </summary>
    /// <param name="action">Callback function</param>
    public new void RemoveListener(UnityAction action)
    {
        base.RemoveListener(action);
        _nonPersistentListenersCount--;
    }

    /// <summary>
    /// Remove all non-persisent listeners from the event.
    /// </summary>
    public new void RemoveAllListeners()
    {
        base.RemoveAllListeners();
        _nonPersistentListenersCount = 0;
    }
}