Skip to content
Snippets Groups Projects
Commit e84203b1 authored by Heine, Eric R's avatar Heine, Eric R
Browse files

Added a component for firing game events on Unity Start and added the ability...

Added a component for firing game events on Unity Start and added the ability to delay firing Unity Events when a Game Event is being listened to.
parent a07b295a
No related branches found
No related tags found
No related merge requests found
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 {
......
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();
}
}
}
fileFormatVersion: 2
guid: cae2d5512c4785a41b7653c7de04f07a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment