UguiEvent.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using Rokid.MRC;
  4. public delegate void ButtonFunc(GameObject obj, object userData);
  5. public delegate void ToggleFunc(GameObject obj, bool isOn);
  6. public delegate void DropdownFunc(GameObject obj, int index);
  7. public delegate void InputFieldFunc(GameObject obj, string val);
  8. public delegate void InputFieldEndFunc(GameObject obj, string val);
  9. public delegate void ScrollRectFunc(GameObject obj, Vector2 pos);
  10. public delegate void SliderFunc(GameObject obj, float val);
  11. // UI绑定事件接口
  12. public static class UguiEvent
  13. {
  14. //主要有Button、Toggle、Dropdown(下拉框)、InputField(输入框)、ScrollRect、Slider
  15. public static void ButtonClick(GameObject go, ButtonFunc func, object userData = null)
  16. {
  17. Button button = go.GetComponent<Button>();
  18. if(button == null)
  19. button = go.AddComponent<Button>();
  20. button.onClick.RemoveAllListeners();
  21. button.onClick.AddListener(delegate ()
  22. {
  23. if(func != null)
  24. {
  25. func(go, userData);
  26. }
  27. });
  28. }
  29. public static void ButtonClick(Transform go, ButtonFunc func, object userData = null)
  30. {
  31. Button button = go.GetComponent<Button>();
  32. if (button == null)
  33. button = go.gameObject.AddComponent<Button>();
  34. button.onClick.RemoveAllListeners();
  35. button.onClick.AddListener(delegate ()
  36. {
  37. if (func != null)
  38. {
  39. func(go.gameObject, userData);
  40. }
  41. });
  42. }
  43. public static void ToggleClick(GameObject go, ToggleFunc func)
  44. {
  45. Toggle toggle = go.GetComponent<Toggle>();
  46. toggle.onValueChanged.RemoveAllListeners();
  47. toggle.onValueChanged.AddListener(delegate (bool isOn)
  48. {
  49. if(func != null)
  50. {
  51. func(go, isOn);
  52. }
  53. });
  54. }
  55. public static void DropdownClick(GameObject go, DropdownFunc func)
  56. {
  57. Dropdown dropdown = go.GetComponent<Dropdown>();
  58. dropdown.onValueChanged.RemoveAllListeners();
  59. dropdown.onValueChanged.AddListener(delegate (int index)
  60. {
  61. if(func != null)
  62. {
  63. func(go, index);
  64. }
  65. });
  66. }
  67. public static void InputFieldChange(GameObject go, InputFieldFunc func)
  68. {
  69. InputField inputField = go.GetComponent<InputField>();
  70. inputField.onValueChanged.RemoveAllListeners();
  71. inputField.onValueChanged.AddListener(delegate (string val)
  72. {
  73. if(func != null)
  74. {
  75. func(go, val);
  76. }
  77. });
  78. }
  79. public static void InputFieldChange(InputField inputField, InputFieldFunc func)
  80. {
  81. if (inputField == null) return;
  82. inputField.onValueChanged.RemoveAllListeners();
  83. inputField.onValueChanged.AddListener(delegate (string val)
  84. {
  85. if (func != null)
  86. {
  87. func(inputField.gameObject, val);
  88. }
  89. });
  90. }
  91. public static void InputFieldEndEdit(GameObject go, InputFieldEndFunc func)
  92. {
  93. InputField inputField = go.GetComponent<InputField>();
  94. inputField.onEndEdit.RemoveAllListeners();
  95. inputField.onEndEdit.AddListener(delegate(string val)
  96. {
  97. if(func != null)
  98. {
  99. func(go, val);
  100. }
  101. });
  102. }
  103. public static void EventTriggerOnClick(GameObject go, EventTriggerListener.VoidDelegate func)
  104. {
  105. EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
  106. if (listener == null) listener = go.AddComponent<EventTriggerListener>();
  107. if (func != null)
  108. {
  109. listener.onClick += func;
  110. }
  111. }
  112. public static void EventTriggerOnEnter(GameObject go, EventTriggerListener.VoidDelegate func)
  113. {
  114. EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
  115. if (listener == null) listener = go.AddComponent<EventTriggerListener>();
  116. if (func != null)
  117. {
  118. listener.onEnter += func;
  119. }
  120. }
  121. public static void EventTriggerOnDown(GameObject go, EventTriggerListener.VoidDelegate func)
  122. {
  123. EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
  124. if(listener == null)
  125. listener = go.AddComponent<EventTriggerListener>();
  126. if(func != null)
  127. {
  128. listener.onDown += func;
  129. }
  130. }
  131. public static void EventTriggerOnUp(GameObject go, EventTriggerListener.VoidDelegate func)
  132. {
  133. EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
  134. if(listener == null)
  135. listener = go.AddComponent<EventTriggerListener>();
  136. if(func != null)
  137. {
  138. listener.onUp += func;
  139. }
  140. }
  141. public static void EventTriggerOnExit(GameObject go, EventTriggerListener.VoidDelegate func)
  142. {
  143. EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
  144. if(listener == null)
  145. listener = go.AddComponent<EventTriggerListener>();
  146. if(func != null)
  147. {
  148. listener.onExit += func;
  149. }
  150. }
  151. public static void EventTriggerOnSelect(GameObject go, EventTriggerListener.VoidDelegate func)
  152. {
  153. EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
  154. if(listener == null)
  155. listener = go.AddComponent<EventTriggerListener>();
  156. if(func != null)
  157. {
  158. listener.onSelect += func;
  159. }
  160. }
  161. public static void EventTriggerOnDragBegin(GameObject go, EventTriggerListener.VoidDelegate func)
  162. {
  163. EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
  164. if(listener == null)
  165. listener = go.AddComponent<EventTriggerListener>();
  166. if(func != null)
  167. {
  168. listener.onDragBegin += func;
  169. }
  170. }
  171. public static void EventTriggerOnDragEnd(GameObject go, EventTriggerListener.VoidDelegate func)
  172. {
  173. EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
  174. if(listener == null)
  175. listener = go.AddComponent<EventTriggerListener>();
  176. if(func != null)
  177. {
  178. listener.onDragEnd += func;
  179. }
  180. }
  181. public static void EventTriggerOnDrag(GameObject go, EventTriggerListener.VoidDelegate func)
  182. {
  183. EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
  184. if(listener == null)
  185. listener = go.AddComponent<EventTriggerListener>();
  186. if(func != null)
  187. {
  188. listener.onDrag += func;
  189. }
  190. }
  191. public static void EventTriggerOnScroll(GameObject go, EventTriggerListener.VoidDelegate func)
  192. {
  193. EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
  194. if(listener == null)
  195. listener = go.AddComponent<EventTriggerListener>();
  196. if(func != null)
  197. {
  198. listener.onScroll += func;
  199. }
  200. }
  201. public static void ScrollRectValueChange(GameObject go, ScrollRectFunc func)
  202. {
  203. ScrollRect scrollRect = go.GetComponent<ScrollRect>();
  204. scrollRect.onValueChanged.RemoveAllListeners();
  205. scrollRect.onValueChanged.AddListener(delegate (Vector2 pos)
  206. {
  207. if(func != null)
  208. {
  209. func(go, pos);
  210. }
  211. });
  212. }
  213. public static void SliderValueChange(GameObject go, SliderFunc func)
  214. {
  215. Slider slider = go.GetComponent<Slider>();
  216. slider.onValueChanged.RemoveAllListeners();
  217. slider.onValueChanged.AddListener(delegate (float val)
  218. {
  219. if(func != null)
  220. {
  221. func(go, val);
  222. }
  223. });
  224. }
  225. public static void SliderValueChange(Transform trans, SliderFunc func)
  226. {
  227. Slider slider = trans.GetComponent<Slider>();
  228. slider.onValueChanged.RemoveAllListeners();
  229. slider.onValueChanged.AddListener(delegate (float val)
  230. {
  231. if (func != null)
  232. {
  233. func(trans.gameObject, val);
  234. }
  235. });
  236. }
  237. /// <summary>
  238. /// 强制重建layout
  239. /// </summary>
  240. public static void ForceRebuildLayout(RectTransform trans)
  241. {
  242. LayoutRebuilder.ForceRebuildLayoutImmediate(trans);
  243. }
  244. }