No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EventTrigger.cs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine.Events;
  4. using UnityEngine.Serialization;
  5. namespace UnityEngine.EventSystems
  6. {
  7. [AddComponentMenu("Event/Event Trigger")]
  8. /// <summary>
  9. /// Receives events from the EventSystem and calls registered functions for each event.
  10. /// </summary>
  11. /// <remarks>
  12. /// The EventTrigger can be used to specify functions you wish to be called for each EventSystem event.
  13. /// You can assign multiple functions to a single event and whenever the EventTrigger receives that event it will call those functions in the order they were provided.
  14. ///
  15. /// NOTE: Attaching this component to a GameObject will make that object intercept ALL events, and no events will propagate to parent objects.
  16. /// </remarks>
  17. /// <example>
  18. /// There are two ways to intercept events: You could extend EventTrigger, and override the functions for the events you are interested in intercepting; as shown in this example:
  19. /// <code>
  20. /// <![CDATA[
  21. /// using UnityEngine;
  22. /// using UnityEngine.EventSystems;
  23. ///
  24. /// public class EventTriggerExample : EventTrigger
  25. /// {
  26. /// public override void OnBeginDrag(PointerEventData data)
  27. /// {
  28. /// Debug.Log("OnBeginDrag called.");
  29. /// }
  30. ///
  31. /// public override void OnCancel(BaseEventData data)
  32. /// {
  33. /// Debug.Log("OnCancel called.");
  34. /// }
  35. ///
  36. /// public override void OnDeselect(BaseEventData data)
  37. /// {
  38. /// Debug.Log("OnDeselect called.");
  39. /// }
  40. ///
  41. /// public override void OnDrag(PointerEventData data)
  42. /// {
  43. /// Debug.Log("OnDrag called.");
  44. /// }
  45. ///
  46. /// public override void OnDrop(PointerEventData data)
  47. /// {
  48. /// Debug.Log("OnDrop called.");
  49. /// }
  50. ///
  51. /// public override void OnEndDrag(PointerEventData data)
  52. /// {
  53. /// Debug.Log("OnEndDrag called.");
  54. /// }
  55. ///
  56. /// public override void OnInitializePotentialDrag(PointerEventData data)
  57. /// {
  58. /// Debug.Log("OnInitializePotentialDrag called.");
  59. /// }
  60. ///
  61. /// public override void OnMove(AxisEventData data)
  62. /// {
  63. /// Debug.Log("OnMove called.");
  64. /// }
  65. ///
  66. /// public override void OnPointerClick(PointerEventData data)
  67. /// {
  68. /// Debug.Log("OnPointerClick called.");
  69. /// }
  70. ///
  71. /// public override void OnPointerDown(PointerEventData data)
  72. /// {
  73. /// Debug.Log("OnPointerDown called.");
  74. /// }
  75. ///
  76. /// public override void OnPointerEnter(PointerEventData data)
  77. /// {
  78. /// Debug.Log("OnPointerEnter called.");
  79. /// }
  80. ///
  81. /// public override void OnPointerExit(PointerEventData data)
  82. /// {
  83. /// Debug.Log("OnPointerExit called.");
  84. /// }
  85. ///
  86. /// public override void OnPointerUp(PointerEventData data)
  87. /// {
  88. /// Debug.Log("OnPointerUp called.");
  89. /// }
  90. ///
  91. /// public override void OnScroll(PointerEventData data)
  92. /// {
  93. /// Debug.Log("OnScroll called.");
  94. /// }
  95. ///
  96. /// public override void OnSelect(BaseEventData data)
  97. /// {
  98. /// Debug.Log("OnSelect called.");
  99. /// }
  100. ///
  101. /// public override void OnSubmit(BaseEventData data)
  102. /// {
  103. /// Debug.Log("OnSubmit called.");
  104. /// }
  105. ///
  106. /// public override void OnUpdateSelected(BaseEventData data)
  107. /// {
  108. /// Debug.Log("OnUpdateSelected called.");
  109. /// }
  110. /// }
  111. /// ]]>
  112. ///</code>
  113. /// or you can specify individual delegates:
  114. /// <code>
  115. /// <![CDATA[
  116. /// using UnityEngine;
  117. /// using UnityEngine.EventSystems;
  118. ///
  119. ///
  120. /// public class EventTriggerDelegateExample : MonoBehaviour
  121. /// {
  122. /// void Start()
  123. /// {
  124. /// EventTrigger trigger = GetComponent<EventTrigger>();
  125. /// EventTrigger.Entry entry = new EventTrigger.Entry();
  126. /// entry.eventID = EventTriggerType.PointerDown;
  127. /// entry.callback.AddListener((data) => { OnPointerDownDelegate((PointerEventData)data); });
  128. /// trigger.triggers.Add(entry);
  129. /// }
  130. ///
  131. /// public void OnPointerDownDelegate(PointerEventData data)
  132. /// {
  133. /// Debug.Log("OnPointerDownDelegate called.");
  134. /// }
  135. /// }
  136. /// ]]>
  137. ///</code>
  138. /// </example>
  139. public class EventTrigger :
  140. MonoBehaviour,
  141. IPointerEnterHandler,
  142. IPointerExitHandler,
  143. IPointerDownHandler,
  144. IPointerUpHandler,
  145. IPointerClickHandler,
  146. IInitializePotentialDragHandler,
  147. IBeginDragHandler,
  148. IDragHandler,
  149. IEndDragHandler,
  150. IDropHandler,
  151. IScrollHandler,
  152. IUpdateSelectedHandler,
  153. ISelectHandler,
  154. IDeselectHandler,
  155. IMoveHandler,
  156. ISubmitHandler,
  157. ICancelHandler
  158. {
  159. [Serializable]
  160. /// <summary>
  161. /// UnityEvent class for Triggers.
  162. /// </summary>
  163. public class TriggerEvent : UnityEvent<BaseEventData>
  164. {}
  165. [Serializable]
  166. /// <summary>
  167. /// An Entry in the EventSystem delegates list.
  168. /// </summary>
  169. /// <remarks>
  170. /// It stores the callback and which event type should this callback be fired.
  171. /// </remarks>
  172. public class Entry
  173. {
  174. /// <summary>
  175. /// What type of event is the associated callback listening for.
  176. /// </summary>
  177. public EventTriggerType eventID = EventTriggerType.PointerClick;
  178. /// <summary>
  179. /// The desired TriggerEvent to be Invoked.
  180. /// </summary>
  181. public TriggerEvent callback = new TriggerEvent();
  182. }
  183. [FormerlySerializedAs("delegates")]
  184. [SerializeField]
  185. private List<Entry> m_Delegates;
  186. [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
  187. [Obsolete("Please use triggers instead (UnityUpgradable) -> triggers", true)]
  188. public List<Entry> delegates { get { return triggers; } set { triggers = value; } }
  189. protected EventTrigger()
  190. {}
  191. /// <summary>
  192. /// All the functions registered in this EventTrigger
  193. /// </summary>
  194. public List<Entry> triggers
  195. {
  196. get
  197. {
  198. if (m_Delegates == null)
  199. m_Delegates = new List<Entry>();
  200. return m_Delegates;
  201. }
  202. set { m_Delegates = value; }
  203. }
  204. private void Execute(EventTriggerType id, BaseEventData eventData)
  205. {
  206. for (int i = 0; i < triggers.Count; ++i)
  207. {
  208. var ent = triggers[i];
  209. if (ent.eventID == id && ent.callback != null)
  210. ent.callback.Invoke(eventData);
  211. }
  212. }
  213. /// <summary>
  214. /// Called by the EventSystem when the pointer enters the object associated with this EventTrigger.
  215. /// </summary>
  216. public virtual void OnPointerEnter(PointerEventData eventData)
  217. {
  218. Execute(EventTriggerType.PointerEnter, eventData);
  219. }
  220. /// <summary>
  221. /// Called by the EventSystem when the pointer exits the object associated with this EventTrigger.
  222. /// </summary>
  223. public virtual void OnPointerExit(PointerEventData eventData)
  224. {
  225. Execute(EventTriggerType.PointerExit, eventData);
  226. }
  227. /// <summary>
  228. /// Called by the EventSystem every time the pointer is moved during dragging.
  229. /// </summary>
  230. public virtual void OnDrag(PointerEventData eventData)
  231. {
  232. Execute(EventTriggerType.Drag, eventData);
  233. }
  234. /// <summary>
  235. /// Called by the EventSystem when an object accepts a drop.
  236. /// </summary>
  237. public virtual void OnDrop(PointerEventData eventData)
  238. {
  239. Execute(EventTriggerType.Drop, eventData);
  240. }
  241. /// <summary>
  242. /// Called by the EventSystem when a PointerDown event occurs.
  243. /// </summary>
  244. public virtual void OnPointerDown(PointerEventData eventData)
  245. {
  246. Execute(EventTriggerType.PointerDown, eventData);
  247. }
  248. /// <summary>
  249. /// Called by the EventSystem when a PointerUp event occurs.
  250. /// </summary>
  251. public virtual void OnPointerUp(PointerEventData eventData)
  252. {
  253. Execute(EventTriggerType.PointerUp, eventData);
  254. }
  255. /// <summary>
  256. /// Called by the EventSystem when a Click event occurs.
  257. /// </summary>
  258. public virtual void OnPointerClick(PointerEventData eventData)
  259. {
  260. Execute(EventTriggerType.PointerClick, eventData);
  261. }
  262. /// <summary>
  263. /// Called by the EventSystem when a Select event occurs.
  264. /// </summary>
  265. public virtual void OnSelect(BaseEventData eventData)
  266. {
  267. Execute(EventTriggerType.Select, eventData);
  268. }
  269. /// <summary>
  270. /// Called by the EventSystem when a new object is being selected.
  271. /// </summary>
  272. public virtual void OnDeselect(BaseEventData eventData)
  273. {
  274. Execute(EventTriggerType.Deselect, eventData);
  275. }
  276. /// <summary>
  277. /// Called by the EventSystem when a new Scroll event occurs.
  278. /// </summary>
  279. public virtual void OnScroll(PointerEventData eventData)
  280. {
  281. Execute(EventTriggerType.Scroll, eventData);
  282. }
  283. /// <summary>
  284. /// Called by the EventSystem when a Move event occurs.
  285. /// </summary>
  286. public virtual void OnMove(AxisEventData eventData)
  287. {
  288. Execute(EventTriggerType.Move, eventData);
  289. }
  290. /// <summary>
  291. /// Called by the EventSystem when the object associated with this EventTrigger is updated.
  292. /// </summary>
  293. public virtual void OnUpdateSelected(BaseEventData eventData)
  294. {
  295. Execute(EventTriggerType.UpdateSelected, eventData);
  296. }
  297. /// <summary>
  298. /// Called by the EventSystem when a drag has been found, but before it is valid to begin the drag.
  299. /// </summary>
  300. public virtual void OnInitializePotentialDrag(PointerEventData eventData)
  301. {
  302. Execute(EventTriggerType.InitializePotentialDrag, eventData);
  303. }
  304. /// <summary>
  305. /// Called before a drag is started.
  306. /// </summary>
  307. public virtual void OnBeginDrag(PointerEventData eventData)
  308. {
  309. Execute(EventTriggerType.BeginDrag, eventData);
  310. }
  311. /// <summary>
  312. /// Called by the EventSystem once dragging ends.
  313. /// </summary>
  314. public virtual void OnEndDrag(PointerEventData eventData)
  315. {
  316. Execute(EventTriggerType.EndDrag, eventData);
  317. }
  318. /// <summary>
  319. /// Called by the EventSystem when a Submit event occurs.
  320. /// </summary>
  321. public virtual void OnSubmit(BaseEventData eventData)
  322. {
  323. Execute(EventTriggerType.Submit, eventData);
  324. }
  325. /// <summary>
  326. /// Called by the EventSystem when a Cancel event occurs.
  327. /// </summary>
  328. public virtual void OnCancel(BaseEventData eventData)
  329. {
  330. Execute(EventTriggerType.Cancel, eventData);
  331. }
  332. }
  333. }