NetMsgEvents.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using KairoEngine.Core;
  5. namespace KairoEngine.Multiplayer
  6. {
  7. public static class NetMsgEvents
  8. {
  9. public static Dictionary<string, System.Action<string, int, uint, NetMsg>> list = new Dictionary<string, System.Action<string, int, uint, NetMsg>>();
  10. public static void StartListening(string title, System.Action<string, int, uint, NetMsg> listener)
  11. {
  12. System.Action<string, int, uint, NetMsg> action = null;
  13. if (list.TryGetValue(title, out action))
  14. {
  15. action += listener;
  16. list[title] = action;
  17. }
  18. else
  19. {
  20. action += listener;
  21. list.Add(title, action);
  22. }
  23. }
  24. public static void StopListening(string title, System.Action<string, int, uint, NetMsg> listener)
  25. {
  26. System.Action<string, int, uint, NetMsg> action = null;
  27. if (list.TryGetValue(title, out action))
  28. {
  29. action -= listener;
  30. list[title] = action;
  31. }
  32. }
  33. public static void Trigger(string title, string msg, int clientId, uint code, NetMsg netMsg)
  34. {
  35. System.Action<string, int, uint, NetMsg> action = null;
  36. if (list.TryGetValue(title, out action))
  37. {
  38. if(action != null) action(msg, clientId, code, netMsg);
  39. }
  40. }
  41. }
  42. }