diff --git a/Scriptable Variables/Listeners/GameEventListener.cs b/Scriptable Variables/Listeners/GameEventListener.cs index 7c89bdf54fb16e96889116e386254c761be40557..6f016326d02eb9ba60d434067c423657e9968515 100644 --- a/Scriptable Variables/Listeners/GameEventListener.cs +++ b/Scriptable Variables/Listeners/GameEventListener.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using UnityEngine; using UnityEngine.Events; @@ -11,6 +12,9 @@ namespace Shared.ScriptableVariables { [Tooltip("The UnityEvent to raise in response to the game event being raised")] public UnityEvent response; + [Tooltip("The delay to have before firing the Unity Events")] + public float delay = 0.0f; + //--------------------------------------------------------------------------- void OnEnable() { eventToListenTo.OnRaised += OnEventRaised; @@ -23,6 +27,24 @@ namespace Shared.ScriptableVariables { //--------------------------------------------------------------------------- public virtual void OnEventRaised() { + // If there isn't a delay (or it's set wrong), just fire the Unity Event right away + if (delay <= 0.0f) { + InvokeUnityEvent(); + } + // Otherwise, wait for the delay and THEN fire the Unity Event + else { + StartCoroutine(DelayInvokeUnityEvent()); + } + } + + //--------------------------------------------------------------------------- + private IEnumerator DelayInvokeUnityEvent() { + yield return new WaitForSeconds(delay); + InvokeUnityEvent(); + } + + //--------------------------------------------------------------------------- + private void InvokeUnityEvent() { if (response != null) { try { response.Invoke(); @@ -37,6 +59,9 @@ namespace Shared.ScriptableVariables { //--------------------------------------------------------------------------- public abstract class GameEventListener<T> : MonoBehaviour { + [Tooltip("The delay to have before firing the Unity Events")] + public float delay = 0.0f; + //--------------------------------------------------------------------------- protected abstract GameEvent<T> GetGameEvent(); @@ -55,6 +80,24 @@ namespace Shared.ScriptableVariables { //--------------------------------------------------------------------------- public virtual void OnEventRaised(T value) { + // If there isn't a delay (or it's set wrong), just fire the Unity Event right away + if (delay <= 0.0f) { + InvokeUnityEvent(value); + } + // Otherwise, wait for the delay and THEN fire the Unity Event + else { + StartCoroutine(DelayInvokeUnityEvent(value)); + } + } + + //--------------------------------------------------------------------------- + private IEnumerator DelayInvokeUnityEvent(T value) { + yield return new WaitForSeconds(delay); + InvokeUnityEvent(value); + } + + //--------------------------------------------------------------------------- + private void InvokeUnityEvent(T value) { var unityEvent = GetUnityEvent(); if (unityEvent != null) { try { diff --git a/Scriptable Variables/Senders/StartEventSender.cs b/Scriptable Variables/Senders/StartEventSender.cs new file mode 100644 index 0000000000000000000000000000000000000000..c30d80104d222e6259ecebb3aad9d47daaec1d76 --- /dev/null +++ b/Scriptable Variables/Senders/StartEventSender.cs @@ -0,0 +1,13 @@ +using UnityEngine; + +namespace Shared.ScriptableVariables { + // Fires the supplied GameEvent on Start() + public class StartEventSender : MonoBehaviour { + [Tooltip("Game Event to fire on start")] + public GameEvent startEvent; + + void Start() { + startEvent?.Raise(); + } + } +} diff --git a/Scriptable Variables/Senders/StartEventSender.cs.meta b/Scriptable Variables/Senders/StartEventSender.cs.meta new file mode 100644 index 0000000000000000000000000000000000000000..61572173f92da1e2b3c59a790342ff85372b2909 --- /dev/null +++ b/Scriptable Variables/Senders/StartEventSender.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: cae2d5512c4785a41b7653c7de04f07a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: