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.

PointerEventData.cs 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. /// <![CDATA[
  122. /// using UnityEngine;
  123. /// using System.Collections;
  124. /// using UnityEngine.UI;
  125. /// using UnityEngine.EventSystems;// Required when using Event data.
  126. ///
  127. /// public class ExampleClass : MonoBehaviour, IPointerDownHandler
  128. /// {
  129. /// public void OnPointerDown(PointerEventData eventData)
  130. /// {
  131. /// //Grab the number of consecutive clicks and assign it to an integer varible.
  132. /// int i = eventData.clickCount;
  133. /// //Display the click count.
  134. /// Debug.Log(i);
  135. /// }
  136. /// }
  137. /// ]]>
  138. ///</code>
  139. /// </example>
  140. public int clickCount { get; set; }
  141. /// <summary>
  142. /// The amount of scroll since the last update.
  143. /// </summary>
  144. public Vector2 scrollDelta { get; set; }
  145. /// <summary>
  146. /// Should a drag threshold be used?
  147. /// </summary>
  148. /// <remarks>
  149. /// If you do not want a drag threshold set this to false in IInitializePotentialDragHandler.OnInitializePotentialDrag.
  150. /// </remarks>
  151. public bool useDragThreshold { get; set; }
  152. /// <summary>
  153. /// Is a drag operation currently occuring.
  154. /// </summary>
  155. public bool dragging { get; set; }
  156. /// <summary>
  157. /// The EventSystems.PointerEventData.InputButton for this event.
  158. /// </summary>
  159. public InputButton button { get; set; }
  160. /// <summary>
  161. /// The amount of pressure currently applied by a touch.
  162. /// </summary>
  163. /// <remarks>
  164. /// If the device does not report pressure, the value of this property is 1.0f.
  165. /// </remarks>
  166. /// <seealso cref="UnityEngine.UIElements.IPointerEvent" />
  167. public float pressure { get; set; }
  168. /// <summary>
  169. /// The pressure applied to an additional pressure-sensitive control on the stylus.
  170. /// </summary>
  171. /// <seealso cref="UnityEngine.UIElements.IPointerEvent" />
  172. public float tangentialPressure { get; set; }
  173. /// <summary>
  174. /// The angle of the stylus relative to the surface, in radians
  175. /// </summary>
  176. /// <remarks>
  177. /// A value of 0 indicates that the stylus is parallel to the surface. A value of pi/2 indicates that it is perpendicular to the surface.
  178. /// </remarks>
  179. /// <seealso cref="UnityEngine.UIElements.IPointerEvent" />
  180. public float altitudeAngle { get; set; }
  181. /// <summary>
  182. /// The angle of the stylus relative to the x-axis, in radians.
  183. /// </summary>
  184. /// <remarks>
  185. /// A value of 0 indicates that the stylus is pointed along the x-axis of the device.
  186. /// </remarks>
  187. /// <seealso cref="UnityEngine.UIElements.IPointerEvent" />
  188. public float azimuthAngle { get; set; }
  189. /// <summary>
  190. /// The rotation of the stylus around its axis, in radians.
  191. /// </summary>
  192. /// <seealso cref="UnityEngine.UIElements.IPointerEvent" />
  193. public float twist { get; set; }
  194. /// <summary>
  195. /// An estimate of the radius of a touch.
  196. /// </summary>
  197. /// <remarks>
  198. /// Add `radiusVariance` to get the maximum touch radius, subtract it to get the minimum touch radius.
  199. /// </remarks>
  200. /// <seealso cref="UnityEngine.UIElements.IPointerEvent" />
  201. public Vector2 radius { get; set; }
  202. /// <summary>
  203. /// The accuracy of the touch radius.
  204. /// </summary>
  205. /// <remarks>
  206. /// Add this value to the radius to get the maximum touch radius, subtract it to get the minimum touch radius.
  207. /// </remarks>
  208. public Vector2 radiusVariance { get; set; }
  209. /// <summary>
  210. /// Specifies in the case of a pointer exit if the pointer has fully exited the area or if it has just entered a child.
  211. /// </summary>
  212. public bool fullyExited { get; set; }
  213. /// <summary>
  214. /// Specifies in the case of a pointer enter if the pointer has entered a new area or if it has just reentered a parent after leaving a child.
  215. /// </summary>
  216. public bool reentered { get; set; }
  217. /// <seealso cref="UnityEngine.UIElements.IPointerEvent" />
  218. public PointerEventData(EventSystem eventSystem) : base(eventSystem)
  219. {
  220. eligibleForClick = false;
  221. pointerId = -1;
  222. position = Vector2.zero; // Current position of the mouse or touch event
  223. delta = Vector2.zero; // Delta since last update
  224. pressPosition = Vector2.zero; // Delta since the event started being tracked
  225. clickTime = 0.0f; // The last time a click event was sent out (used for double-clicks)
  226. clickCount = 0; // Number of clicks in a row. 2 for a double-click for example.
  227. scrollDelta = Vector2.zero;
  228. useDragThreshold = true;
  229. dragging = false;
  230. button = InputButton.Left;
  231. pressure = 0f;
  232. tangentialPressure = 0f;
  233. altitudeAngle = 0f;
  234. azimuthAngle = 0f;
  235. twist = 0f;
  236. radius = Vector2.zero;
  237. radiusVariance = Vector2.zero;
  238. }
  239. /// <summary>
  240. /// Is the pointer moving.
  241. /// </summary>
  242. public bool IsPointerMoving()
  243. {
  244. return delta.sqrMagnitude > 0.0f;
  245. }
  246. /// <summary>
  247. /// Is scroll being used on the input device.
  248. /// </summary>
  249. public bool IsScrolling()
  250. {
  251. return scrollDelta.sqrMagnitude > 0.0f;
  252. }
  253. /// <summary>
  254. /// The camera associated with the last OnPointerEnter event.
  255. /// </summary>
  256. public Camera enterEventCamera
  257. {
  258. get { return pointerCurrentRaycast.module == null ? null : pointerCurrentRaycast.module.eventCamera; }
  259. }
  260. /// <summary>
  261. /// The camera associated with the last OnPointerPress event.
  262. /// </summary>
  263. public Camera pressEventCamera
  264. {
  265. get { return pointerPressRaycast.module == null ? null : pointerPressRaycast.module.eventCamera; }
  266. }
  267. /// <summary>
  268. /// The GameObject that received the OnPointerDown.
  269. /// </summary>
  270. public GameObject pointerPress
  271. {
  272. get { return m_PointerPress; }
  273. set
  274. {
  275. if (m_PointerPress == value)
  276. return;
  277. lastPress = m_PointerPress;
  278. m_PointerPress = value;
  279. }
  280. }
  281. public override string ToString()
  282. {
  283. var sb = new StringBuilder();
  284. sb.AppendLine("<b>Position</b>: " + position);
  285. sb.AppendLine("<b>delta</b>: " + delta);
  286. sb.AppendLine("<b>eligibleForClick</b>: " + eligibleForClick);
  287. sb.AppendLine("<b>pointerEnter</b>: " + pointerEnter);
  288. sb.AppendLine("<b>pointerPress</b>: " + pointerPress);
  289. sb.AppendLine("<b>lastPointerPress</b>: " + lastPress);
  290. sb.AppendLine("<b>pointerDrag</b>: " + pointerDrag);
  291. sb.AppendLine("<b>Use Drag Threshold</b>: " + useDragThreshold);
  292. sb.AppendLine("<b>Current Raycast:</b>");
  293. sb.AppendLine(pointerCurrentRaycast.ToString());
  294. sb.AppendLine("<b>Press Raycast:</b>");
  295. sb.AppendLine(pointerPressRaycast.ToString());
  296. sb.AppendLine("<b>pressure</b>: " + pressure);
  297. sb.AppendLine("<b>tangentialPressure</b>: " + tangentialPressure);
  298. sb.AppendLine("<b>altitudeAngle</b>: " + altitudeAngle);
  299. sb.AppendLine("<b>azimuthAngle</b>: " + azimuthAngle);
  300. sb.AppendLine("<b>twist</b>: " + twist);
  301. sb.AppendLine("<b>radius</b>: " + radius);
  302. sb.AppendLine("<b>radiusVariance</b>: " + radiusVariance);
  303. return sb.ToString();
  304. }
  305. }
  306. }