RequestEvents.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace KairoEngine.Core
  5. {
  6. public class RequestEvents
  7. {
  8. #region Request_bool_with_no_args
  9. public Dictionary<string, System.Func<bool>> list = new Dictionary<string, System.Func<bool>>();
  10. public void Bind(string title, System.Func<bool> listener)
  11. {
  12. System.Func<bool> action = null;
  13. if (list.TryGetValue(title, out action) == false)
  14. {
  15. action += listener;
  16. list.Add(title, action);
  17. }
  18. }
  19. public void Unbind(string title, System.Func<bool> listener)
  20. {
  21. System.Func<bool> action = null;
  22. if (list.TryGetValue(title, out action))
  23. {
  24. action -= listener;
  25. list.Remove(title);
  26. }
  27. }
  28. // public bool Get(string title)
  29. // {
  30. // System.Func<bool> action = null;
  31. // if (list.TryGetValue(title, out action))
  32. // {
  33. // if(action != null) return action();
  34. // }
  35. // return false;
  36. // }
  37. public EventResponse<bool> GetBool(string title)
  38. {
  39. System.Func<bool> action = null;
  40. EventResponse<bool> response = new EventResponse<bool>();
  41. if (list.TryGetValue(title, out action))
  42. {
  43. if(action != null)
  44. {
  45. bool result = action();
  46. response.value = result;
  47. response.status = EventResponseStatus.OK;
  48. }
  49. }
  50. else response.status = EventResponseStatus.NotFound;
  51. return response;
  52. }
  53. #endregion
  54. #region Request_GameObject_with_no_args
  55. public Dictionary<string, System.Func<GameObject>> list2 = new Dictionary<string, System.Func<GameObject>>();
  56. public void Bind(string title, System.Func<GameObject> listener)
  57. {
  58. System.Func<GameObject> action = null;
  59. if (list2.TryGetValue(title, out action) == false)
  60. {
  61. action += listener;
  62. list2.Add(title, action);
  63. }
  64. }
  65. public void Unbind(string title, System.Func<GameObject> listener)
  66. {
  67. System.Func<GameObject> action = null;
  68. if (list2.TryGetValue(title, out action))
  69. {
  70. action -= listener;
  71. list2.Remove(title);
  72. }
  73. }
  74. public EventResponse<GameObject> GetGameObject(string title)
  75. {
  76. System.Func<GameObject> action = null;
  77. EventResponse<GameObject> response = new EventResponse<GameObject>();
  78. if (list2.TryGetValue(title, out action))
  79. {
  80. if(action != null)
  81. {
  82. GameObject result = action();
  83. response.value = result;
  84. if(response.value == null) response.status = EventResponseStatus.EmptyResponse;
  85. else response.status = EventResponseStatus.OK;
  86. }
  87. }
  88. else response.status = EventResponseStatus.NotFound;
  89. return response;
  90. }
  91. #endregion
  92. }
  93. }