Created
December 20, 2021 01:23
-
-
Save st4rdog/7ef3d8875d292570411f0dc56555c989 to your computer and use it in GitHub Desktop.
Event/Messaging system for C# - strongly typed
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
// 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