Skip to content

Instantly share code, notes, and snippets.

@st4rdog
Created December 20, 2021 01:23
Show Gist options
  • Save st4rdog/7ef3d8875d292570411f0dc56555c989 to your computer and use it in GitHub Desktop.
Save st4rdog/7ef3d8875d292570411f0dc56555c989 to your computer and use it in GitHub Desktop.
Event/Messaging system for C# - strongly typed
// Created by lordofduct - https://forum.unity.com/threads/recommended-event-system.856294/#post-5644981
//
// Usage example:
//
// public delegate void OnDiedEvent(GameObject go);
//
// Messaging<OnDiedEvent>.Trigger?.Invoke(gameObject);
//
// Messaging<OnDiedEvent>.Register(go => {
// Debug.Log($"{go.name} died.");
// });
using System;
public static class Messaging<T> where T : System.Delegate
{
public static void Register(T callback) => Trigger = System.Delegate.Combine(Trigger, callback) as T;
public static void Unregister(T callback) => Trigger = System.Delegate.Remove(Trigger, callback) as T;
public static T Trigger { get; private set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment