Skip to content

Instantly share code, notes, and snippets.

@willis81808
Last active February 6, 2024 01:21
Show Gist options
  • Select an option

  • Save willis81808/f63f8a5ad3a8f04d9d1397de59c80f5f to your computer and use it in GitHub Desktop.

Select an option

Save willis81808/f63f8a5ad3a8f04d9d1397de59c80f5f to your computer and use it in GitHub Desktop.
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