Last active
September 17, 2018 22:59
-
-
Save mattmccray/2819d8fcf275137adab598dd0ceec918 to your computer and use it in GitHub Desktop.
Simple event base class for Unity.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.Events; | |
namespace WizBangify | |
{ | |
public delegate void Subscription(bool add); | |
public abstract class BaseGameEvent<T> where T : BaseGameEvent<T> | |
{ | |
private bool hasFired; | |
public delegate void EventListener(T info); | |
public static event EventListener listeners; | |
public static void AddListener(EventListener listener) | |
{ | |
listeners -= listener; // Prevents duplicates... | |
listeners += listener; | |
} | |
public static void RemoveListener(EventListener listener) | |
{ | |
listeners -= listener; | |
} | |
public static Subscription Subscribe(EventListener listener, bool addByDefault = true) | |
{ | |
if (addByDefault) | |
{ | |
AddListener(listener); | |
} | |
return (bool addListener) => | |
{ | |
if (addListener) | |
{ | |
AddListener(listener); | |
} | |
else | |
{ | |
RemoveListener(listener); | |
} | |
}; | |
} | |
public static Subscription SubscriptionFor(EventListener listener) | |
{ | |
return Subscribe(listener, false); | |
} | |
public string Name | |
{ | |
get | |
{ | |
return this.GetType().Name; | |
} | |
} | |
protected T FireEvent() | |
{ | |
if (hasFired) | |
{ | |
throw new Exception("This event has already fired, to prevent infinite loops you can't refire an event"); | |
} | |
Debug.Log("[Firing event: " + this.Name + "]"); | |
hasFired = true; | |
if (listeners != null) | |
{ | |
listeners(this as T); | |
} | |
return this as T; | |
} | |
} | |
public class SubscriptionManager | |
{ | |
private List<Subscription> subscriptions = new List<Subscription>(); | |
public int Count | |
{ | |
get | |
{ | |
return this.subscriptions.Count; | |
} | |
} | |
public void Add(Subscription subscription) | |
{ | |
subscriptions.Add(subscription); | |
} | |
public void AddAll(params Subscription[] bulkSubscriptions) | |
{ | |
foreach (Subscription subscription in bulkSubscriptions) | |
{ | |
subscriptions.Add(subscription); | |
} | |
} | |
public bool Remove(Subscription subscription) | |
{ | |
return subscriptions.Remove(subscription); | |
} | |
public void UnsubscribeAll() | |
{ | |
foreach (Subscription subscription in subscriptions) | |
{ | |
subscription(false); | |
} | |
} | |
public void SubscribeAll() | |
{ | |
foreach (Subscription subscription in subscriptions) | |
{ | |
subscription(true); | |
} | |
} | |
public void Clear() | |
{ | |
subscriptions.Clear(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GameStartEvent : WizBangify.BaseGameEvent<GameStartEvent> | |
{ | |
// You can add parameters too, if you like... | |
static public GameStartEvent Fire() { | |
return new GameStartEvent().FireEvent(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.Events; | |
namespace WizBangify | |
{ | |
public class ExampleUsage : MonoBehaviour | |
{ | |
private SubscriptionManager subscriptions = new SubscriptionManager(); | |
void Awake() | |
{ | |
// Subscribe to event | |
subscriptions.Add(GameStartEvent.SubScribe(OnGameStart, false)) | |
} | |
void Start() | |
{ | |
// At some point, someone Fires the event (like this)... | |
GameStartEvent.Fire(); | |
} | |
void OnGameStart() | |
{ | |
// Handle event... | |
} | |
void OnEnable() | |
{ | |
subscriptions.SubscribeAll(); | |
} | |
void OnDisable() | |
{ | |
subscriptions.UnsubscribeAll(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment