63 lines
1.7 KiB
C#
63 lines
1.7 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
#if UNITY_EDITOR
|
|
using TriInspector;
|
|
#endif
|
|
|
|
namespace R0bbie.Timeline.Events
|
|
{
|
|
/// <summary>
|
|
/// Scriptable Object to handle in game events without the need of direct references
|
|
/// </summary>
|
|
[CreateAssetMenu(fileName = "GameEvent", menuName = "Timeline/Game Event")]
|
|
public class GameEvent : ScriptableObject
|
|
{
|
|
// Private Variables
|
|
|
|
List<IGameEventListener> listeners = new List<IGameEventListener>();
|
|
|
|
/// <summary>
|
|
/// Call all listeners registered under this event
|
|
/// </summary>
|
|
public void Raise()
|
|
{
|
|
for (int i = listeners.Count -1; i >= 0; i--)
|
|
listeners[i].OnEventRaised();
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Register listener to be called when event raised
|
|
/// </summary>
|
|
/// <param name="listener"></param>
|
|
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);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Unregister listener to not be called anymore
|
|
/// </summary>
|
|
/// <param name="listener"></param>
|
|
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
|
|
|
|
}
|
|
}
|