Skip to content

Instantly share code, notes, and snippets.

@TheLouisHong
Last active July 13, 2023 12:28
Show Gist options
  • Save TheLouisHong/1d5f123ac1097e8f4c055cd5cbf9a438 to your computer and use it in GitHub Desktop.
Save TheLouisHong/1d5f123ac1097e8f4c055cd5cbf9a438 to your computer and use it in GitHub Desktop.
SimpleMessage: A performant alternative to SendMessage in Unity. (A simple wrapper around the Unity UI event system)
using UnityEngine;
using UnityEngine.EventSystems;
public static class SimpleMessage
{
public delegate void EventFunction<T1,T2>(T1 handler, T2 eventData);
public static bool Send<T,T2>(GameObject target, object eventData, EventFunction<T, T2> functor)
where T : IEventSystemHandler
{
return ExecuteEvents.Execute<T>(target, null, (x, y) => functor(x, (T2)eventData));
}
public static void SendHierarchy<T, T2>(GameObject root, object eventData, EventFunction<T,T2> functor) where T : IEventSystemHandler
{
ExecuteEvents.ExecuteHierarchy<T>(root, null, (x, y) => functor(x, (T2)eventData));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment