GenericEvents.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace KairoEngine.Core
  5. {
  6. public class GenericEvents
  7. {
  8. #region Empty_Event
  9. public static Dictionary<string, System.Action> list = new Dictionary<string, System.Action>();
  10. public static void StartListening(string title, System.Action listener)
  11. {
  12. System.Action 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 listener)
  25. {
  26. System.Action 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)
  34. {
  35. System.Action action = null;
  36. if (list.TryGetValue(title, out action))
  37. {
  38. if(action != null) action();
  39. }
  40. }
  41. #endregion
  42. #region String_Event
  43. public static Dictionary<string, System.Action<string>> listString = new Dictionary<string, System.Action<string>>();
  44. public static void StartListening(string title, System.Action<string> listener)
  45. {
  46. System.Action<string> action = null;
  47. if (listString.TryGetValue(title, out action))
  48. {
  49. action += listener;
  50. listString[title] = action;
  51. }
  52. else
  53. {
  54. action += listener;
  55. listString.Add(title, action);
  56. }
  57. }
  58. public static void StopListening(string title, System.Action<string> listener)
  59. {
  60. System.Action<string> action = null;
  61. if (listString.TryGetValue(title, out action))
  62. {
  63. action -= listener;
  64. listString[title] = action;
  65. }
  66. }
  67. public static void Trigger(string title, string text)
  68. {
  69. System.Action<string> action = null;
  70. if (listString.TryGetValue(title, out action))
  71. {
  72. if(action != null) action(text);
  73. }
  74. }
  75. #endregion
  76. #region Float_Event
  77. public static Dictionary<string, System.Action<float>> list2 = new Dictionary<string, System.Action<float>>();
  78. public static void StartListening(string title, System.Action<float> listener)
  79. {
  80. System.Action<float> action = null;
  81. if (list2.TryGetValue(title, out action))
  82. {
  83. action += listener;
  84. list2[title] = action;
  85. }
  86. else
  87. {
  88. action += listener;
  89. list2.Add(title, action);
  90. }
  91. }
  92. public static void StopListening(string title, System.Action<float> listener)
  93. {
  94. System.Action<float> action = null;
  95. if (list2.TryGetValue(title, out action))
  96. {
  97. action -= listener;
  98. list2[title] = action;
  99. }
  100. }
  101. public static void Trigger(string title, float value)
  102. {
  103. System.Action<float> action = null;
  104. if (list2.TryGetValue(title, out action))
  105. {
  106. if(action != null) action(value);
  107. }
  108. }
  109. #endregion
  110. #region Float_Float_Event
  111. public static Dictionary<string, System.Action<float, float>> list3 = new Dictionary<string, System.Action<float, float>>();
  112. public static void StartListening(string title, System.Action<float, float> listener)
  113. {
  114. System.Action<float, float> action = null;
  115. if (list3.TryGetValue(title, out action))
  116. {
  117. action += listener;
  118. list3[title] = action;
  119. }
  120. else
  121. {
  122. action += listener;
  123. list3.Add(title, action);
  124. }
  125. }
  126. public static void StopListening(string title, System.Action<float, float> listener)
  127. {
  128. System.Action<float, float> action = null;
  129. if (list3.TryGetValue(title, out action))
  130. {
  131. action -= listener;
  132. list3[title] = action;
  133. }
  134. }
  135. public static void Trigger(string title, float value1, float value2)
  136. {
  137. System.Action<float, float> action = null;
  138. if (list3.TryGetValue(title, out action))
  139. {
  140. if(action != null) action(value1, value2);
  141. }
  142. }
  143. #endregion
  144. #region Int_Event
  145. public static Dictionary<string, System.Action<int>> list4 = new Dictionary<string, System.Action<int>>();
  146. public static void StartListening(string title, System.Action<int> listener)
  147. {
  148. System.Action<int> action = null;
  149. if (list4.TryGetValue(title, out action))
  150. {
  151. action += listener;
  152. list4[title] = action;
  153. }
  154. else
  155. {
  156. action += listener;
  157. list4.Add(title, action);
  158. }
  159. }
  160. public static void StopListening(string title, System.Action<int> listener)
  161. {
  162. System.Action<int> action = null;
  163. if (list4.TryGetValue(title, out action))
  164. {
  165. action -= listener;
  166. list4[title] = action;
  167. }
  168. }
  169. public static void Trigger(string title, int value)
  170. {
  171. System.Action<int> action = null;
  172. if (list4.TryGetValue(title, out action))
  173. {
  174. if(action != null) action(value);
  175. }
  176. }
  177. #endregion
  178. #region String_Int_Event
  179. public static Dictionary<string, System.Action<string, int>> list5 = new Dictionary<string, System.Action<string, int>>();
  180. public static void StartListening(string title, System.Action<string, int> listener)
  181. {
  182. System.Action<string, int> action = null;
  183. if (list5.TryGetValue(title, out action))
  184. {
  185. action += listener;
  186. list5[title] = action;
  187. }
  188. else
  189. {
  190. action += listener;
  191. list5.Add(title, action);
  192. }
  193. }
  194. public static void StopListening(string title, System.Action<string, int> listener)
  195. {
  196. System.Action<string, int> action = null;
  197. if (list5.TryGetValue(title, out action))
  198. {
  199. action -= listener;
  200. list5[title] = action;
  201. }
  202. }
  203. public static void Trigger(string title, string text, int value)
  204. {
  205. System.Action<string, int> action = null;
  206. if (list5.TryGetValue(title, out action))
  207. {
  208. if(action != null) action(text, value);
  209. }
  210. }
  211. #endregion
  212. #region String_float_Event
  213. public static Dictionary<string, System.Action<string, float>> stringFloatList = new Dictionary<string, System.Action<string, float>>();
  214. public static void StartListeningForStringFloat(string title, System.Action<string, float> listener) => StartListening(title, listener);
  215. public static void StartListening(string title, System.Action<string, float> listener)
  216. {
  217. System.Action<string, float> action = null;
  218. if (stringFloatList.TryGetValue(title, out action))
  219. {
  220. action += listener;
  221. stringFloatList[title] = action;
  222. }
  223. else
  224. {
  225. action += listener;
  226. stringFloatList.Add(title, action);
  227. }
  228. }
  229. public static void StopListeningForStringFloat(string title, System.Action<string, float> listener) => StopListening(title, listener);
  230. public static void StopListening(string title, System.Action<string, float> listener)
  231. {
  232. System.Action<string, float> action = null;
  233. if (stringFloatList.TryGetValue(title, out action))
  234. {
  235. action -= listener;
  236. stringFloatList[title] = action;
  237. }
  238. }
  239. public static void Trigger(string title, string text, float value)
  240. {
  241. System.Action<string, float> action = null;
  242. if (stringFloatList.TryGetValue(title, out action))
  243. {
  244. if(action != null) action(text, value);
  245. }
  246. }
  247. #endregion
  248. #region String_string_Event
  249. public static Dictionary<string, System.Action<string, string>> stringStringList = new Dictionary<string, System.Action<string, string>>();
  250. public static void StartListening(string title, System.Action<string, string> listener)
  251. {
  252. System.Action<string, string> action = null;
  253. if (stringStringList.TryGetValue(title, out action))
  254. {
  255. action += listener;
  256. stringStringList[title] = action;
  257. }
  258. else
  259. {
  260. action += listener;
  261. stringStringList.Add(title, action);
  262. }
  263. }
  264. public static void StopListening(string title, System.Action<string, string> listener)
  265. {
  266. System.Action<string, string> action = null;
  267. if (stringStringList.TryGetValue(title, out action))
  268. {
  269. action -= listener;
  270. stringStringList[title] = action;
  271. }
  272. }
  273. public static void Trigger(string title, string text, string value)
  274. {
  275. System.Action<string, string> action = null;
  276. if (stringStringList.TryGetValue(title, out action))
  277. {
  278. if(action != null) action(text, value);
  279. }
  280. }
  281. #endregion
  282. #region ScriptableObject_Event
  283. public static Dictionary<string, System.Action<ScriptableObject>> list6 = new Dictionary<string, System.Action<ScriptableObject>>();
  284. public static void StartListening(string title, System.Action<ScriptableObject> listener)
  285. {
  286. System.Action<ScriptableObject> action = null;
  287. if (list6.TryGetValue(title, out action))
  288. {
  289. action += listener;
  290. list6[title] = action;
  291. }
  292. else
  293. {
  294. action += listener;
  295. list6.Add(title, action);
  296. }
  297. }
  298. public static void StopListening(string title, System.Action<ScriptableObject> listener)
  299. {
  300. System.Action<ScriptableObject> action = null;
  301. if (list6.TryGetValue(title, out action))
  302. {
  303. action -= listener;
  304. list6[title] = action;
  305. }
  306. }
  307. public static void Trigger(string title, ScriptableObject scriptableObject)
  308. {
  309. System.Action<ScriptableObject> action = null;
  310. if (list6.TryGetValue(title, out action))
  311. {
  312. if(action != null) action(scriptableObject);
  313. }
  314. }
  315. #endregion
  316. #region String_Vector3_Event
  317. public static Dictionary<string, System.Action<string, Vector3>> listStringVector3 = new Dictionary<string, System.Action<string, Vector3>>();
  318. public static void StartListening(string title, System.Action<string, Vector3> listener)
  319. {
  320. System.Action<string, Vector3> action = null;
  321. if (listStringVector3.TryGetValue(title, out action))
  322. {
  323. action += listener;
  324. listStringVector3[title] = action;
  325. }
  326. else
  327. {
  328. action += listener;
  329. listStringVector3.Add(title, action);
  330. }
  331. }
  332. public static void StopListening(string title, System.Action<string, Vector3> listener)
  333. {
  334. System.Action<string, Vector3> action = null;
  335. if (listStringVector3.TryGetValue(title, out action))
  336. {
  337. action -= listener;
  338. listStringVector3[title] = action;
  339. }
  340. }
  341. public static void Trigger(string title, string text, Vector3 value)
  342. {
  343. System.Action<string, Vector3> action = null;
  344. if (listStringVector3.TryGetValue(title, out action))
  345. {
  346. if(action != null) action(text, value);
  347. }
  348. }
  349. #endregion
  350. }
  351. }