PointerEventData.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. using System;
  2. using System.Text;
  3. using System.Collections.Generic;
  4. namespace UnityEngine.EventSystems
  5. {
  6. /// <summary>
  7. /// Each touch event creates one of these containing all the relevant information.
  8. /// </summary>
  9. public class PointerEventData : BaseEventData
  10. {
  11. /// <summary>
  12. /// Input press tracking.
  13. /// </summary>
  14. public enum InputButton
  15. {
  16. /// <summary>
  17. /// Left button
  18. /// </summary>
  19. Left = 0,
  20. /// <summary>
  21. /// Right button.
  22. /// </summary>
  23. Right = 1,
  24. /// <summary>
  25. /// Middle button
  26. /// </summary>
  27. Middle = 2
  28. }
  29. /// <summary>
  30. /// The state of a press for the given frame.
  31. /// </summary>
  32. public enum FramePressState
  33. {
  34. /// <summary>
  35. /// Button was pressed this frame.
  36. /// </summary>
  37. Pressed,
  38. /// <summary>
  39. /// Button was released this frame.
  40. /// </summary>
  41. Released,
  42. /// <summary>
  43. /// Button was pressed and released this frame.
  44. /// </summary>
  45. PressedAndReleased,
  46. /// <summary>
  47. /// Same as last frame.
  48. /// </summary>
  49. NotChanged
  50. }
  51. /// <summary>
  52. /// The object that received 'OnPointerEnter'.
  53. /// </summary>
  54. public GameObject pointerEnter { get; set; }
  55. // The object that received OnPointerDown
  56. private GameObject m_PointerPress;
  57. /// <summary>
  58. /// The raw GameObject for the last press event. This means that it is the 'pressed' GameObject even if it can not receive the press event itself.
  59. /// </summary>
  60. public GameObject lastPress { get; private set; }
  61. /// <summary>
  62. /// The object that the press happened on even if it can not handle the press event.
  63. /// </summary>
  64. public GameObject rawPointerPress { get; set; }
  65. /// <summary>
  66. /// The object that is receiving 'OnDrag'.
  67. /// </summary>
  68. public GameObject pointerDrag { get; set; }
  69. /// <summary>
  70. /// The object that should receive the 'OnPointerClick' event.
  71. /// </summary>
  72. public GameObject pointerClick { get; set; }
  73. /// <summary>
  74. /// RaycastResult associated with the current event.
  75. /// </summary>
  76. public RaycastResult pointerCurrentRaycast { get; set; }
  77. /// <summary>
  78. /// RaycastResult associated with the pointer press.
  79. /// </summary>
  80. public RaycastResult pointerPressRaycast { get; set; }
  81. public List<GameObject> hovered = new List<GameObject>();
  82. /// <summary>
  83. /// Is it possible to click this frame
  84. /// </summary>
  85. public bool eligibleForClick { get; set; }
  86. /// <summary>
  87. /// Id of the pointer (touch id).
  88. /// </summary>
  89. public int pointerId { get; set; }
  90. /// <summary>
  91. /// Current pointer position.
  92. /// </summary>
  93. public Vector2 position { get; set; }
  94. /// <summary>
  95. /// Pointer delta since last update.
  96. /// </summary>
  97. public Vector2 delta { get; set; }
  98. /// <summary>
  99. /// Position of the press.
  100. /// </summary>
  101. public Vector2 pressPosition { get; set; }
  102. /// <summary>
  103. /// World-space position where a ray cast into the screen hits something
  104. /// </summary>
  105. [Obsolete("Use either pointerCurrentRaycast.worldPosition or pointerPressRaycast.worldPosition")]
  106. public Vector3 worldPosition { get; set; }
  107. /// <summary>
  108. /// World-space normal where a ray cast into the screen hits something
  109. /// </summary>
  110. [Obsolete("Use either pointerCurrentRaycast.worldNormal or pointerPressRaycast.worldNormal")]
  111. public Vector3 worldNormal { get; set; }
  112. /// <summary>
  113. /// The last time a click event was sent. Used for double click
  114. /// </summary>
  115. public float clickTime { get; set; }
  116. /// <summary>
  117. /// Number of clicks in a row.
  118. /// </summary>
  119. /// <example>
  120. /// <code>
  121. /// using UnityEngine;
  122. /// using System.Collections;
  123. /// using UnityEngine.UI;
  124. /// using UnityEngine.EventSystems;// Required when using Event data.
  125. ///
  126. /// public class ExampleClass : MonoBehaviour, IPointerDownHandler
  127. /// {
  128. /// public void OnPointerDown(PointerEventData eventData)
  129. /// {
  130. /// //Grab the number of consecutive clicks and assign it to an integer varible.
  131. /// int i = eventData.clickCount;
  132. /// //Display the click count.
  133. /// Debug.Log(i);
  134. /// }
  135. /// }
  136. /// </code>
  137. /// </example>
  138. public int clickCount { get; set; }
  139. /// <summary>
  140. /// The amount of scroll since the last update.
  141. /// </summary>
  142. public Vector2 scrollDelta { get; set; }
  143. /// <summary>
  144. /// Should a drag threshold be used?
  145. /// </summary>
  146. /// <remarks>
  147. /// If you do not want a drag threshold set this to false in IInitializePotentialDragHandler.OnInitializePotentialDrag.
  148. /// </remarks>
  149. public bool useDragThreshold { get; set; }
  150. /// <summary>
  151. /// Is a drag operation currently occuring.
  152. /// </summary>
  153. public bool dragging { get; set; }
  154. /// <summary>
  155. /// The EventSystems.PointerEventData.InputButton for this event.
  156. /// </summary>
  157. public InputButton button { get; set; }
  158. public PointerEventData(EventSystem eventSystem) : base(eventSystem)
  159. {
  160. eligibleForClick = false;
  161. pointerId = -1;
  162. position = Vector2.zero; // Current position of the mouse or touch event
  163. delta = Vector2.zero; // Delta since last update
  164. pressPosition = Vector2.zero; // Delta since the event started being tracked
  165. clickTime = 0.0f; // The last time a click event was sent out (used for double-clicks)
  166. clickCount = 0; // Number of clicks in a row. 2 for a double-click for example.
  167. scrollDelta = Vector2.zero;
  168. useDragThreshold = true;
  169. dragging = false;
  170. button = InputButton.Left;
  171. }
  172. /// <summary>
  173. /// Is the pointer moving.
  174. /// </summary>
  175. public bool IsPointerMoving()
  176. {
  177. return delta.sqrMagnitude > 0.0f;
  178. }
  179. /// <summary>
  180. /// Is scroll being used on the input device.
  181. /// </summary>
  182. public bool IsScrolling()
  183. {
  184. return scrollDelta.sqrMagnitude > 0.0f;
  185. }
  186. /// <summary>
  187. /// The camera associated with the last OnPointerEnter event.
  188. /// </summary>
  189. public Camera enterEventCamera
  190. {
  191. get { return pointerCurrentRaycast.module == null ? null : pointerCurrentRaycast.module.eventCamera; }
  192. }
  193. /// <summary>
  194. /// The camera associated with the last OnPointerPress event.
  195. /// </summary>
  196. public Camera pressEventCamera
  197. {
  198. get { return pointerPressRaycast.module == null ? null : pointerPressRaycast.module.eventCamera; }
  199. }
  200. /// <summary>
  201. /// The GameObject that received the OnPointerDown.
  202. /// </summary>
  203. public GameObject pointerPress
  204. {
  205. get { return m_PointerPress; }
  206. set
  207. {
  208. if (m_PointerPress == value)
  209. return;
  210. lastPress = m_PointerPress;
  211. m_PointerPress = value;
  212. }
  213. }
  214. public override string ToString()
  215. {
  216. var sb = new StringBuilder();
  217. sb.AppendLine("<b>Position</b>: " + position);
  218. sb.AppendLine("<b>delta</b>: " + delta);
  219. sb.AppendLine("<b>eligibleForClick</b>: " + eligibleForClick);
  220. sb.AppendLine("<b>pointerEnter</b>: " + pointerEnter);
  221. sb.AppendLine("<b>pointerPress</b>: " + pointerPress);
  222. sb.AppendLine("<b>lastPointerPress</b>: " + lastPress);
  223. sb.AppendLine("<b>pointerDrag</b>: " + pointerDrag);
  224. sb.AppendLine("<b>Use Drag Threshold</b>: " + useDragThreshold);
  225. sb.AppendLine("<b>Current Raycast:</b>");
  226. sb.AppendLine(pointerCurrentRaycast.ToString());
  227. sb.AppendLine("<b>Press Raycast:</b>");
  228. sb.AppendLine(pointerPressRaycast.ToString());
  229. return sb.ToString();
  230. }
  231. }
  232. }