Last active
February 6, 2024 01:21
-
-
Save willis81808/f63f8a5ad3a8f04d9d1397de59c80f5f to your computer and use it in GitHub Desktop.
This file contains hidden or 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; | |
| using System.Collections.Generic; | |
| using UnityEngine; | |
| namespace EventDispatcher | |
| { | |
| public abstract class Event<T> where T : Event<T> | |
| { | |
| private bool hasFired; | |
| public delegate void EventListener(T info); | |
| private static event EventListener listeners; | |
| public static void RegisterListener(EventListener listener) | |
| { | |
| listeners += listener; | |
| } | |
| public static void UnregisterListener(EventListener listener) | |
| { | |
| listeners -= listener; | |
| } | |
| public void FireEvent() | |
| { | |
| if (hasFired) | |
| { | |
| throw new Exception("This event has already fired, to prevent infinite loops you can't refire an event"); | |
| } | |
| hasFired = true; | |
| listeners?.Invoke(this as T); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment