Skip to content
Snippets Groups Projects
Commit 075dedab authored by Guerrero, Michael J's avatar Guerrero, Michael J
Browse files

Added a coroutine listener for easy event waiting inside a coroutine.

parent 1381d545
No related branches found
No related tags found
No related merge requests found
using UnityEngine; using System.Collections;
using UnityEngine;
namespace Shared.ScriptableVariables { namespace Shared.ScriptableVariables {
// ScriptableVariable that decouples components firing and listening to game events // ScriptableVariable that decouples components firing and listening to game events
...@@ -7,12 +8,22 @@ namespace Shared.ScriptableVariables { ...@@ -7,12 +8,22 @@ namespace Shared.ScriptableVariables {
public delegate void GameEventRaised(); public delegate void GameEventRaised();
public event GameEventRaised OnRaised; public event GameEventRaised OnRaised;
int lastEventRaisedFrame = 0;
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
[ContextMenu("Raise")] [ContextMenu("Raise")]
public void Raise() { public void Raise() {
if (OnRaised != null) { if (OnRaised != null) {
OnRaised(); OnRaised();
} }
lastEventRaisedFrame = Time.frameCount;
}
//---------------------------------------------------------------------------
public IEnumerator WaitForEvent() {
while (Time.frameCount != lastEventRaisedFrame) {
yield return null;
}
} }
} }
...@@ -21,11 +32,22 @@ namespace Shared.ScriptableVariables { ...@@ -21,11 +32,22 @@ namespace Shared.ScriptableVariables {
public delegate void GameEventRaised(T value); public delegate void GameEventRaised(T value);
public event GameEventRaised OnRaised; public event GameEventRaised OnRaised;
int lastEventRaisedFrame = 0;
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
public void Raise(T value) { public void Raise(T value) {
if (OnRaised != null) { if (OnRaised != null) {
OnRaised(value); OnRaised(value);
} }
lastEventRaisedFrame = Time.frameCount;
}
//---------------------------------------------------------------------------
public IEnumerator WaitForEvent() {
while (Time.frameCount != lastEventRaisedFrame) {
yield return null;
}
} }
} }
} }
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
"name": "com.futuretech.shared", "name": "com.futuretech.shared",
"displayName": "FutureTech Shared", "displayName": "FutureTech Shared",
"description": "Contains shared items such as the Scriptable Variables.", "description": "Contains shared items such as the Scriptable Variables.",
"version": "0.1.8", "version": "0.1.9",
"unity": "2018.3", "unity": "2018.3",
"license": "MIT", "license": "MIT",
"repository": { "repository": {
......
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