Skip to content

Instantly share code, notes, and snippets.

@insominx
Created May 7, 2014 03:12
Show Gist options
  • Save insominx/8823991dba760b5da0ff to your computer and use it in GitHub Desktop.
Save insominx/8823991dba760b5da0ff to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
namespace Util {
// =========================================================================
public class EventListener<T> where T: GameEvent {
public T receivedEvent;
// -------------------------------------------------------------------------
public IEnumerator Listen() {
receivedEvent = null;
EventMessenger.Instance.AddListener<T>(OnReceiveEvent);
do
{
yield return null;
} while (receivedEvent == null);
EventMessenger.Instance.RemoveListener<T>(OnReceiveEvent);
}
// -------------------------------------------------------------------------
public T TakeEvent() {
T eventToReturn = receivedEvent;
receivedEvent = null;
return eventToReturn;
}
// -------------------------------------------------------------------------
void OnReceiveEvent(T newEvent) {
receivedEvent = newEvent;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment