EventInterfaces.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. namespace UnityEngine.EventSystems
  2. {
  3. /// <summary>
  4. /// Base class that all EventSystem events inherit from.
  5. /// </summary>
  6. public interface IEventSystemHandler
  7. {
  8. }
  9. /// <summary>
  10. /// Interface to implement if you wish to receive OnPointerEnter callbacks.
  11. /// </summary>
  12. /// <remarks>
  13. /// Criteria for this event is implementation dependent. For example see StandAloneInputModule.
  14. /// </remarks>
  15. public interface IPointerEnterHandler : IEventSystemHandler
  16. {
  17. /// <summary>
  18. /// Use this callback to detect pointer enter events
  19. /// </summary>
  20. void OnPointerEnter(PointerEventData eventData);
  21. }
  22. /// <summary>
  23. /// Interface to implement if you wish to receive OnPointerExit callbacks.
  24. /// </summary>
  25. /// <remarks>
  26. /// Criteria for this event is implementation dependent. For example see StandAloneInputModule.
  27. /// </remarks>
  28. public interface IPointerExitHandler : IEventSystemHandler
  29. {
  30. /// <summary>
  31. /// Use this callback to detect pointer exit events
  32. /// </summary>
  33. void OnPointerExit(PointerEventData eventData);
  34. }
  35. /// <summary>
  36. /// Interface to implement if you wish to receive OnPointerDown callbacks.
  37. /// </summary>
  38. /// <remarks>
  39. /// Criteria for this event is implementation dependent. For example see StandAloneInputModule.
  40. /// </remarks>
  41. public interface IPointerDownHandler : IEventSystemHandler
  42. {
  43. /// <summary>
  44. /// Use this callback to detect pointer down events.
  45. /// </summary>
  46. void OnPointerDown(PointerEventData eventData);
  47. }
  48. /// <summary>
  49. /// Interface to implement if you wish to receive OnPointerUp callbacks.
  50. /// Note: In order to receive OnPointerUp callbacks, you must also implement the EventSystems.IPointerDownHandler|IPointerDownHandler interface
  51. /// </summary>
  52. /// <remarks>
  53. /// Criteria for this event is implementation dependent. For example see StandAloneInputModule.
  54. /// </remarks>
  55. public interface IPointerUpHandler : IEventSystemHandler
  56. {
  57. /// <summary>
  58. /// Use this callback to detect pointer up events.
  59. /// </summary>
  60. void OnPointerUp(PointerEventData eventData);
  61. }
  62. /// <summary>
  63. /// Interface to implement if you wish to receive OnPointerClick callbacks.
  64. /// </summary>
  65. /// <remarks>
  66. /// Criteria for this event is implementation dependent. For example see StandAloneInputModule.
  67. /// </remarks>
  68. /// <remarks>
  69. /// Use the IPointerClickHandler Interface to handle click input using OnPointerClick callbacks. Ensure an Event System exists in the Scene to allow click detection. For click detection on non-UI GameObjects, ensure a EventSystems.PhysicsRaycaster is attached to the Camera.
  70. /// </remarks>
  71. /// <example>
  72. /// <code>
  73. /// using UnityEngine;
  74. /// using UnityEngine.EventSystems;
  75. ///
  76. /// public class Example : MonoBehaviour, IPointerClickHandler
  77. /// {
  78. /// //Detect if a click occurs
  79. /// public void OnPointerClick(PointerEventData pointerEventData)
  80. /// {
  81. /// //Output to console the clicked GameObject's name and the following message. You can replace this with your own actions for when clicking the GameObject.
  82. /// Debug.Log(name + " Game Object Clicked!");
  83. /// }
  84. /// }
  85. /// </code>
  86. /// </example>
  87. public interface IPointerClickHandler : IEventSystemHandler
  88. {
  89. /// <summary>
  90. /// Use this callback to detect clicks.
  91. /// </summary>
  92. void OnPointerClick(PointerEventData eventData);
  93. }
  94. /// <summary>
  95. /// Interface to implement if you wish to receive OnBeginDrag callbacks.
  96. /// Note: You need to implement IDragHandler in addition to IBeginDragHandler.
  97. /// </summary>
  98. /// <remarks>
  99. /// Criteria for this event is implementation dependent. For example see StandAloneInputModule.
  100. /// </remarks>
  101. public interface IBeginDragHandler : IEventSystemHandler
  102. {
  103. /// <summary>
  104. /// Called by a BaseInputModule before a drag is started.
  105. /// </summary>
  106. void OnBeginDrag(PointerEventData eventData);
  107. }
  108. /// <summary>
  109. /// Interface to implement if you wish to receive OnInitializePotentialDrag callbacks.
  110. /// </summary>
  111. /// <remarks>
  112. /// Criteria for this event is implementation dependent. For example see StandAloneInputModule.
  113. /// </remarks>
  114. public interface IInitializePotentialDragHandler : IEventSystemHandler
  115. {
  116. /// <summary>
  117. /// Called by a BaseInputModule when a drag has been found but before it is valid to begin the drag.
  118. /// </summary>
  119. void OnInitializePotentialDrag(PointerEventData eventData);
  120. }
  121. /// <summary>
  122. /// Interface to implement if you wish to receive OnDrag callbacks.
  123. /// </summary>
  124. /// <remarks>
  125. /// Criteria for this event is implementation dependent. For example see StandAloneInputModule.
  126. /// </remarks>
  127. /// <example>
  128. /// <code>
  129. /// using UnityEngine;
  130. /// using UnityEngine.EventSystems;
  131. /// using UnityEngine.UI;
  132. ///
  133. /// [RequireComponent(typeof(Image))]
  134. /// public class DragMe : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
  135. /// {
  136. /// public bool dragOnSurfaces = true;
  137. ///
  138. /// private GameObject m_DraggingIcon;
  139. /// private RectTransform m_DraggingPlane;
  140. ///
  141. /// public void OnBeginDrag(PointerEventData eventData)
  142. /// {
  143. /// var canvas = FindInParents<Canvas>(gameObject);
  144. /// if (canvas == null)
  145. /// return;
  146. ///
  147. /// // We have clicked something that can be dragged.
  148. /// // What we want to do is create an icon for this.
  149. /// m_DraggingIcon = new GameObject("icon");
  150. ///
  151. /// m_DraggingIcon.transform.SetParent(canvas.transform, false);
  152. /// m_DraggingIcon.transform.SetAsLastSibling();
  153. ///
  154. /// var image = m_DraggingIcon.AddComponent<Image>();
  155. ///
  156. /// image.sprite = GetComponent<Image>().sprite;
  157. /// image.SetNativeSize();
  158. ///
  159. /// if (dragOnSurfaces)
  160. /// m_DraggingPlane = transform as RectTransform;
  161. /// else
  162. /// m_DraggingPlane = canvas.transform as RectTransform;
  163. ///
  164. /// SetDraggedPosition(eventData);
  165. /// }
  166. ///
  167. /// public void OnDrag(PointerEventData data)
  168. /// {
  169. /// if (m_DraggingIcon != null)
  170. /// SetDraggedPosition(data);
  171. /// }
  172. ///
  173. /// private void SetDraggedPosition(PointerEventData data)
  174. /// {
  175. /// if (dragOnSurfaces && data.pointerEnter != null && data.pointerEnter.transform as RectTransform != null)
  176. /// m_DraggingPlane = data.pointerEnter.transform as RectTransform;
  177. ///
  178. /// var rt = m_DraggingIcon.GetComponent<RectTransform>();
  179. /// Vector3 globalMousePos;
  180. /// if (RectTransformUtility.ScreenPointToWorldPointInRectangle(m_DraggingPlane, data.position, data.pressEventCamera, out globalMousePos))
  181. /// {
  182. /// rt.position = globalMousePos;
  183. /// rt.rotation = m_DraggingPlane.rotation;
  184. /// }
  185. /// }
  186. ///
  187. /// public void OnEndDrag(PointerEventData eventData)
  188. /// {
  189. /// if (m_DraggingIcon != null)
  190. /// Destroy(m_DraggingIcon);
  191. /// }
  192. ///
  193. /// static public T FindInParents<T>(GameObject go) where T : Component
  194. /// {
  195. /// if (go == null) return null;
  196. /// var comp = go.GetComponent<T>();
  197. ///
  198. /// if (comp != null)
  199. /// return comp;
  200. ///
  201. /// Transform t = go.transform.parent;
  202. /// while (t != null && comp == null)
  203. /// {
  204. /// comp = t.gameObject.GetComponent<T>();
  205. /// t = t.parent;
  206. /// }
  207. /// return comp;
  208. /// }
  209. /// }
  210. /// </code>
  211. /// </example>
  212. public interface IDragHandler : IEventSystemHandler
  213. {
  214. /// <summary>
  215. /// When dragging is occurring this will be called every time the cursor is moved.
  216. /// </summary>
  217. void OnDrag(PointerEventData eventData);
  218. }
  219. /// <summary>
  220. /// Interface to implement if you wish to receive OnEndDrag callbacks.
  221. /// Note: You need to implement IDragHandler in addition to IEndDragHandler.
  222. /// </summary>
  223. /// <remarks>
  224. /// Criteria for this event is implementation dependent. For example see StandAloneInputModule.
  225. /// </remarks>
  226. public interface IEndDragHandler : IEventSystemHandler
  227. {
  228. /// <summary>
  229. /// Called by a BaseInputModule when a drag is ended.
  230. /// </summary>
  231. void OnEndDrag(PointerEventData eventData);
  232. }
  233. /// <summary>
  234. /// Interface to implement if you wish to receive OnDrop callbacks.
  235. /// </summary>
  236. /// <example>
  237. /// <code>
  238. /// using UnityEngine;
  239. /// using UnityEngine.EventSystems;
  240. ///
  241. /// public class DropMe : MonoBehaviour, IDropHandler
  242. /// {
  243. /// public void OnDrop(PointerEventData data)
  244. /// {
  245. /// if (data.pointerDrag != null)
  246. /// {
  247. /// Debug.Log ("Dropped object was: " + data.pointerDrag);
  248. /// }
  249. /// }
  250. /// }
  251. /// </code>
  252. /// </example>
  253. /// <remarks>
  254. /// Criteria for this event is implementation dependent. For example see StandAloneInputModule.
  255. /// </remarks>
  256. public interface IDropHandler : IEventSystemHandler
  257. {
  258. /// <summary>
  259. /// Called by a BaseInputModule on a target that can accept a drop.
  260. /// </summary>
  261. void OnDrop(PointerEventData eventData);
  262. }
  263. /// <summary>
  264. /// Interface to implement if you wish to receive OnScroll callbacks.
  265. /// </summary>
  266. /// <remarks>
  267. /// Criteria for this event is implementation dependent. For example see StandAloneInputModule.
  268. /// </remarks>
  269. public interface IScrollHandler : IEventSystemHandler
  270. {
  271. /// <summary>
  272. /// Use this callback to detect scroll events.
  273. /// </summary>
  274. void OnScroll(PointerEventData eventData);
  275. }
  276. /// <summary>
  277. /// Interface to implement if you wish to receive OnUpdateSelected callbacks.
  278. /// </summary>
  279. /// <remarks>
  280. /// Criteria for this event is implementation dependent. For example see StandAloneInputModule.
  281. /// </remarks>
  282. public interface IUpdateSelectedHandler : IEventSystemHandler
  283. {
  284. /// <summary>
  285. /// Called by the EventSystem when the object associated with this EventTrigger is updated.
  286. /// </summary>
  287. /// <example>
  288. /// <code>
  289. /// using UnityEngine;
  290. /// using UnityEngine.EventSystems;
  291. ///
  292. /// public class UpdateSelectedExample : MonoBehaviour, IUpdateSelectedHandler
  293. /// {
  294. /// public void OnUpdateSelected(BaseEventData data)
  295. /// {
  296. /// Debug.Log("OnUpdateSelected called.");
  297. /// }
  298. /// }
  299. /// </code>
  300. /// </example>
  301. void OnUpdateSelected(BaseEventData eventData);
  302. }
  303. /// <summary>
  304. /// Interface to implement if you wish to receive OnSelect callbacks.
  305. /// </summary>
  306. /// <remarks>
  307. /// Criteria for this event is implementation dependent. For example see StandAloneInputModule.
  308. /// </remarks>
  309. public interface ISelectHandler : IEventSystemHandler
  310. {
  311. void OnSelect(BaseEventData eventData);
  312. }
  313. /// <summary>
  314. /// Interface to implement if you wish to receive OnDeselect callbacks.
  315. /// </summary>
  316. /// <remarks>
  317. /// Criteria for this event is implementation dependent. For example see StandAloneInputModule.
  318. /// </remarks>
  319. public interface IDeselectHandler : IEventSystemHandler
  320. {
  321. /// <summary>
  322. /// Called by the EventSystem when a new object is being selected.
  323. /// </summary>
  324. void OnDeselect(BaseEventData eventData);
  325. }
  326. /// <summary>
  327. /// Interface to implement if you wish to receive OnMove callbacks.
  328. /// </summary>
  329. /// <remarks>
  330. /// Criteria for this event is implementation dependent. For example see StandAloneInputModule.
  331. /// </remarks>
  332. public interface IMoveHandler : IEventSystemHandler
  333. {
  334. /// <summary>
  335. /// Called by a BaseInputModule when a move event occurs.
  336. /// </summary>
  337. void OnMove(AxisEventData eventData);
  338. }
  339. /// <summary>
  340. /// Interface to implement if you wish to receive OnSubmit callbacks.
  341. /// </summary>
  342. /// <remarks>
  343. /// Criteria for this event is implementation dependent. For example see StandAloneInputModule.
  344. /// </remarks>
  345. public interface ISubmitHandler : IEventSystemHandler
  346. {
  347. void OnSubmit(BaseEventData eventData);
  348. }
  349. /// <summary>
  350. /// Interface to implement if you wish to receive OnCancel callbacks.
  351. /// </summary>
  352. /// <remarks>
  353. /// Criteria for this event is implementation dependent. For example see StandAloneInputModule.
  354. /// </remarks>
  355. public interface ICancelHandler : IEventSystemHandler
  356. {
  357. void OnCancel(BaseEventData eventData);
  358. }
  359. }