using System.Collections.Generic; using UnityEngine; #if UNITY_EDITOR using TriInspector; #endif namespace R0bbie.Timeline.Events { /// /// Scriptable Object to handle in game events without the need of direct references /// [CreateAssetMenu(fileName = "GameEvent", menuName = "Timeline/Game Event")] public class GameEvent : ScriptableObject { // Private Variables List listeners = new List(); /// /// Call all listeners registered under this event /// public void Raise() { for (int i = listeners.Count -1; i >= 0; i--) listeners[i].OnEventRaised(); } /// /// Register listener to be called when event raised /// /// public void RegisterListener(IGameEventListener listener) { // Check that the listener is not on the list before adding it to avoid duplicates if(!listeners.Contains(listener)) listeners.Add(listener); } /// /// Unregister listener to not be called anymore /// /// public void UnregisterListener(IGameEventListener listener) { // Check that the listener is part of the list before trying to remove it if(listeners.Contains(listener)) listeners.Remove(listener); } #if UNITY_EDITOR [Button("Test Raise")] void Test() { Raise(); } #endif } }