InteractionEvents.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace KairoEngine
  5. {
  6. public class EventData
  7. {
  8. public GameObject obj;
  9. public EventData(GameObject obj = null)
  10. {
  11. this.obj = obj;
  12. }
  13. }
  14. public class InteractionEvents
  15. {
  16. public static Dictionary<string, System.Action<EventData>> list = new Dictionary<string, System.Action<EventData>>();
  17. public static void StartListening(string title, System.Action<EventData> listener)
  18. {
  19. System.Action<EventData> action = null;
  20. if (list.TryGetValue(title, out action))
  21. {
  22. action += listener;
  23. list[title] = action;
  24. }
  25. else
  26. {
  27. action += listener;
  28. list.Add(title, action);
  29. }
  30. }
  31. public static void StopListening(string title, System.Action<EventData> listener)
  32. {
  33. System.Action<EventData> action = null;
  34. if (list.TryGetValue(title, out action))
  35. {
  36. action -= listener;
  37. list[title] = action;
  38. }
  39. }
  40. public static void Trigger(string title, EventData eventData)
  41. {
  42. System.Action<EventData> action = null;
  43. if (list.TryGetValue(title, out action))
  44. {
  45. if(action != null) action(eventData);
  46. }
  47. }
  48. }
  49. }