GenericEventsInventoryExtensions.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using KairoEngine.Core;
  5. namespace KairoEngine.Inventory
  6. {
  7. public static class GenericEventsInventoryExtensions
  8. {
  9. public static Dictionary<string, System.Action<ItemContainer, ItemRef>> list = new Dictionary<string, System.Action<ItemContainer, ItemRef>>();
  10. public static void StartListening(this GenericEvents e, string title, System.Action<ItemContainer, ItemRef> listener)
  11. {
  12. System.Action<ItemContainer, ItemRef> 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(this GenericEvents e,string title, System.Action<ItemContainer, ItemRef> listener)
  25. {
  26. System.Action<ItemContainer, ItemRef> action = null;
  27. if (list.TryGetValue(title, out action))
  28. {
  29. action -= listener;
  30. list[title] = action;
  31. }
  32. }
  33. public static void Trigger(this GenericEvents e, string title, ItemContainer container, ItemRef item)
  34. {
  35. System.Action<ItemContainer, ItemRef> action = null;
  36. if (list.TryGetValue(title, out action))
  37. {
  38. if(action != null) action(container, item);
  39. }
  40. }
  41. }
  42. }