Skip to content

Instantly share code, notes, and snippets.

@dnovacik
Created December 28, 2021 22:47
Show Gist options
  • Save dnovacik/3e68002e1c9d14b29b9703b3f8383b18 to your computer and use it in GitHub Desktop.
Save dnovacik/3e68002e1c9d14b29b9703b3f8383b18 to your computer and use it in GitHub Desktop.
EventBroker
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Assets.Scripts.Events
{
public static class EventBroker
{
public delegate void EventCallback<T>(T data);
public delegate void EventCallback<T, T1>(T data, T1 arg);
public delegate void EventCallback<T, T1, T2>(T data, T1 arg, T2 arg1);
public delegate void EventCallback();
private static Dictionary<(string, Type), List<Delegate>> _events = new Dictionary<(string, Type), List<Delegate>>();
public static void RegisterEvent(string eventName, EventCallback listener)
{
if (_events.TryGetValue((eventName, null), out var listeners))
{
if (!listeners.Contains(listener))
{
listeners.Add(listener);
}
}
else
{
_events.Add((eventName, null), new List<Delegate> { listener });
}
}
public static void RegisterEvent<T>(string eventName, EventCallback<T> listener)
{
if (_events.TryGetValue((eventName, typeof(T)), out var listeners))
{
if (!listeners.Contains(listener))
{
listeners.Add(listener);
}
}
else
{
_events.Add((eventName, typeof(T)), new List<Delegate> { listener });
}
}
public static void RegisterEvent<T>(string eventName, EventCallback listener)
{
if (_events.TryGetValue((eventName, typeof(T)), out var listeners))
{
if (!listeners.Contains(listener))
{
listeners.Add(listener);
}
}
else
{
_events.Add((eventName, typeof(T)), new List<Delegate> { listener });
}
}
public static void RegisterEvent<T, T1>(string eventName, EventCallback<T, T1> listener)
{
if (_events.TryGetValue((eventName, typeof(T)), out var listeners))
{
if (!listeners.Contains(listener))
{
listeners.Add(listener);
}
}
else
{
_events.Add((eventName, typeof(T)), new List<Delegate> { listener });
}
}
public static void RegisterEvent<T, T1>(string eventName, EventCallback<T1> listener)
{
if (_events.TryGetValue((eventName, typeof(T)), out var listeners))
{
if (!listeners.Contains(listener))
{
listeners.Add(listener);
}
}
else
{
_events.Add((eventName, typeof(T)), new List<Delegate> { listener });
}
}
public static void RegisterEvent<T, T1, T2>(string eventName, EventCallback<T, T1, T2> listener)
{
if (_events.TryGetValue((eventName, typeof(T)), out var listeners))
{
if (!listeners.Contains(listener))
{
listeners.Add(listener);
}
}
else
{
_events.Add((eventName, typeof(T)), new List<Delegate> { listener });
}
}
public static void UnregisterEvent(string eventName, EventCallback listener)
{
if (_events.TryGetValue((eventName, null), out var listeners))
{
listeners.Remove(listener);
}
}
public static void UnregisterEvent<T>(string eventName, EventCallback<T> listener)
{
if (_events.TryGetValue((eventName, typeof(T)), out var listeners))
{
listeners.Remove(listener);
}
}
public static void UnregisterEvent<T, T1>(string eventName, EventCallback<T, T1> listener)
{
if (_events.TryGetValue((eventName, typeof(T)), out var listeners))
{
listeners.Remove(listener);
}
}
public static void UnregisterEvent<T, T1>(string eventName, EventCallback<T1> listener)
{
if (_events.TryGetValue((eventName, typeof(T)), out var listeners))
{
listeners.Remove(listener);
}
}
public static void UnregisterEvent<T, T1, T2>(string eventName, EventCallback<T, T1, T2> listener)
{
if (_events.TryGetValue((eventName, typeof(T)), out var listeners))
{
listeners.Remove(listener);
}
}
public static void ExecuteEvent(string eventName)
{
if (_events.TryGetValue((eventName, null), out var listeners))
{
listeners.ForEach(listener =>
{
listener.DynamicInvoke();
});
}
}
public static void ExecuteEvent<T>(string eventName, T data)
{
if (_events.TryGetValue((eventName, typeof(T)), out var listeners))
{
listeners.ForEach(listener =>
{
listener.DynamicInvoke(data);
});
}
}
public static void ExecuteEvent<T, T1>(string eventName, T data, T1 arg)
{
if (_events.TryGetValue((eventName, typeof(T)), out var listeners))
{
listeners.ForEach(listener =>
{
listener.DynamicInvoke(data, arg);
});
}
}
public static void ExecuteEvent<T, T1>(string eventName, T1 arg)
{
if (_events.TryGetValue((eventName, typeof(T)), out var listeners))
{
listeners.ForEach(listener =>
{
listener.DynamicInvoke(arg);
});
}
}
public static void ExecuteEvent<T, T1, T2>(string eventName, T data, T1 arg, T2 arg1)
{
if (_events.TryGetValue((eventName, typeof(T)), out var listeners))
{
listeners.ForEach(listener =>
{
listener.DynamicInvoke(data, arg, arg1);
});
}
}
public static void ExecuteEvent<T>(string eventName)
{
if (_events.TryGetValue((eventName, typeof(T)), out var listeners))
{
listeners.ForEach(listener =>
{
listener.DynamicInvoke();
});
}
}
}
}
using UnityEngine;
namespace Assets.Scripts.Extensions
{
public static class MonoBehaviorExtensions
{
public static void SetupSingleton<TSingleton>(this TSingleton wokenInstance, ref TSingleton singletonInstance, bool persist = false)
where TSingleton : MonoBehaviour
{
if (singletonInstance != null && singletonInstance != wokenInstance)
{
// if new script instance is awaken & different instance of the same already exists, destroy the new
Object.Destroy(wokenInstance.gameObject);
return;
}
singletonInstance = wokenInstance;
if (persist)
{
var parentTransform = singletonInstance.transform;
while (parentTransform.parent != null)
{
parentTransform = parentTransform.parent;
}
Object.DontDestroyOnLoad(parentTransform.gameObject);
}
}
}
}
using Assets.Scripts.Events;
using Assets.Scripts.Extensions;
using Assets.Scripts.Models.Player;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace Assets.Scripts.Managers
{
public class PlayerManager : MonoBehaviour
{
public static PlayerManager Instance;
private string Test { get; set; }
private void Awake()
{
this.SetupSingleton(ref Instance, true);
EventBroker.RegisterEvent<string>("EventTest", OnEventReceived);
}
private void OnDestroy()
{
EventBroker.UnregisterEvent<string>("EventTest", OnEventReceived);
}
private void OnEventReceived(string data)
{
this.Test = data;
}
}
}
using Assets.Scripts.Events;
using Assets.Scripts.Models.Player;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace Assets.Scripts.Player
{
public class PlayerController : MonoBehaviour
{
private void Awake()
{
}
private void Start()
{
EventBroker.ExecuteEvent("EventTest", "This is about to be set as a Test prop on player manager");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment