Без опису
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. /// The index of the display that this pointer event comes from.
  88. /// </summary>
  89. public int displayIndex { get; set; }
  90. /// <summary>
  91. /// Id of the pointer (touch id).
  92. /// </summary>
  93. public int pointerId { get; set; }
  94. /// <summary>
  95. /// Current pointer position.
  96. /// </summary>
  97. public Vector2 position { get; set; }
  98. /// <summary>
  99. /// Pointer delta since last update.
  100. /// </summary>
  101. public Vector2 delta { get; set; }
  102. /// <summary>
  103. /// Position of the press.
  104. /// </summary>
  105. public Vector2 pressPosition { get; set; }
  106. /// <summary>
  107. /// World-space position where a ray cast into the screen hits something
  108. /// </summary>
  109. [Obsolete("Use either pointerCurrentRaycast.worldPosition or pointerPressRaycast.worldPosition")]
  110. public Vector3 worldPosition { get; set; }
  111. /// <summary>
  112. /// World-space normal where a ray cast into the screen hits something
  113. /// </summary>
  114. [Obsolete("Use either pointerCurrentRaycast.worldNormal or pointerPressRaycast.worldNormal")]
  115. public Vector3 worldNormal { get; set; }
  116. /// <summary>
  117. /// The last time a click event was sent. Used for double click
  118. /// </summary>
  119. public float clickTime { get; set; }
  120. /// <summary>
  121. /// Number of clicks in a row.
  122. /// </summary>
  123. /// <example>
  124. /// <code>
  125. /// <![CDATA[
  126. /// using UnityEngine;
  127. /// using System.Collections;
  128. /// using UnityEngine.UI;
  129. /// using UnityEngine.EventSystems;// Required when using Event data.
  130. ///
  131. /// public class ExampleClass : MonoBehaviour, IPointerDownHandler
  132. /// {
  133. /// public void OnPointerDown(PointerEventData eventData)
  134. /// {
  135. /// //Grab the number of consecutive clicks and assign it to an integer varible.
  136. /// int i = eventData.clickCount;
  137. /// //Display the click count.
  138. /// Debug.Log(i);
  139. /// }
  140. /// }
  141. /// ]]>
  142. ///</code>
  143. /// </example>
  144. public int clickCount { get; set; }
  145. /// <summary>
  146. /// The amount of scroll since the last update.
  147. /// </summary>
  148. public Vector2 scrollDelta { get; set; }
  149. /// <summary>
  150. /// Should a drag threshold be used?
  151. /// </summary>
  152. /// <remarks>
  153. /// If you do not want a drag threshold set this to false in IInitializePotentialDragHandler.OnInitializePotentialDrag.
  154. /// </remarks>
  155. public bool useDragThreshold { get; set; }
  156. /// <summary>
  157. /// Is a drag operation currently occuring.
  158. /// </summary>
  159. public bool dragging { get; set; }
  160. /// <summary>
  161. /// The EventSystems.PointerEventData.InputButton for this event.
  162. /// </summary>
  163. public InputButton button { get; set; }
  164. /// <summary>
  165. /// The amount of pressure currently applied by a touch.
  166. /// </summary>
  167. /// <remarks>
  168. /// If the device does not report pressure, the value of this property is 1.0f.
  169. /// </remarks>
  170. /// <seealso cref="UnityEngine.UIElements.IPointerEvent" />
  171. public float pressure { get; set; }
  172. /// <summary>
  173. /// The pressure applied to an additional pressure-sensitive control on the stylus.
  174. /// </summary>
  175. /// <seealso cref="UnityEngine.UIElements.IPointerEvent" />
  176. public float tangentialPressure { get; set; }
  177. /// <summary>
  178. /// The angle of the stylus relative to the surface, in radians
  179. /// </summary>
  180. /// <remarks>
  181. /// 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.
  182. /// </remarks>
  183. /// <seealso cref="UnityEngine.UIElements.IPointerEvent" />
  184. public float altitudeAngle { get; set; }
  185. /// <summary>
  186. /// The angle of the stylus relative to the x-axis, in radians.
  187. /// </summary>
  188. /// <remarks>
  189. /// A value of 0 indicates that the stylus is pointed along the x-axis of the device.
  190. /// </remarks>
  191. /// <seealso cref="UnityEngine.UIElements.IPointerEvent" />
  192. public float azimuthAngle { get; set; }
  193. /// <summary>
  194. /// The rotation of the stylus around its axis, in radians.
  195. /// </summary>
  196. /// <seealso cref="UnityEngine.UIElements.IPointerEvent" />
  197. public float twist { get; set; }
  198. /// <summary>
  199. /// Specifies the angle of the pen relative to the X & Y axis, in radians.
  200. /// </summary>
  201. /// <seealso cref="UnityEngine.UIElements.IPointerEvent" />
  202. public Vector2 tilt { get; set; }
  203. /// <summary>
  204. /// Specifies the state of the pen. For example, whether the pen is in contact with the screen or tablet, whether the pen is inverted, and whether buttons are pressed.
  205. /// </summary>
  206. /// <seealso cref="UnityEngine.UIElements.IPointerEvent" />
  207. public PenStatus penStatus { get; set; }
  208. /// <summary>
  209. /// An estimate of the radius of a touch.
  210. /// </summary>
  211. /// <remarks>
  212. /// Add `radiusVariance` to get the maximum touch radius, subtract it to get the minimum touch radius.
  213. /// </remarks>
  214. /// <seealso cref="UnityEngine.UIElements.IPointerEvent" />
  215. public Vector2 radius { get; set; }
  216. /// <summary>
  217. /// The accuracy of the touch radius.
  218. /// </summary>
  219. /// <remarks>
  220. /// Add this value to the radius to get the maximum touch radius, subtract it to get the minimum touch radius.
  221. /// </remarks>
  222. public Vector2 radiusVariance { get; set; }
  223. /// <summary>
  224. /// Specifies in the case of a pointer exit if the pointer has fully exited the area or if it has just entered a child.
  225. /// </summary>
  226. public bool fullyExited { get; set; }
  227. /// <summary>
  228. /// 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.
  229. /// </summary>
  230. public bool reentered { get; set; }
  231. /// <seealso cref="UnityEngine.UIElements.IPointerEvent" />
  232. public PointerEventData(EventSystem eventSystem) : base(eventSystem)
  233. {
  234. eligibleForClick = false;
  235. displayIndex = 0;
  236. pointerId = -1;
  237. position = Vector2.zero; // Current position of the mouse or touch event
  238. delta = Vector2.zero; // Delta since last update
  239. pressPosition = Vector2.zero; // Delta since the event started being tracked
  240. clickTime = 0.0f; // The last time a click event was sent out (used for double-clicks)
  241. clickCount = 0; // Number of clicks in a row. 2 for a double-click for example.
  242. scrollDelta = Vector2.zero;
  243. useDragThreshold = true;
  244. dragging = false;
  245. button = InputButton.Left;
  246. pressure = 0f;
  247. tangentialPressure = 0f;
  248. altitudeAngle = 0f;
  249. azimuthAngle = 0f;
  250. twist = 0f;
  251. tilt = new Vector2(0f, 0f);
  252. penStatus = PenStatus.None;
  253. radius = Vector2.zero;
  254. radiusVariance = Vector2.zero;
  255. }
  256. /// <summary>
  257. /// Is the pointer moving.
  258. /// </summary>
  259. public bool IsPointerMoving()
  260. {
  261. return delta.sqrMagnitude > 0.0f;
  262. }
  263. /// <summary>
  264. /// Is scroll being used on the input device.
  265. /// </summary>
  266. public bool IsScrolling()
  267. {
  268. return scrollDelta.sqrMagnitude > 0.0f;
  269. }
  270. /// <summary>
  271. /// The camera associated with the last OnPointerEnter event.
  272. /// </summary>
  273. public Camera enterEventCamera
  274. {
  275. get { return pointerCurrentRaycast.module == null ? null : pointerCurrentRaycast.module.eventCamera; }
  276. }
  277. /// <summary>
  278. /// The camera associated with the last OnPointerPress event.
  279. /// </summary>
  280. public Camera pressEventCamera
  281. {
  282. get { return pointerPressRaycast.module == null ? null : pointerPressRaycast.module.eventCamera; }
  283. }
  284. /// <summary>
  285. /// The GameObject that received the OnPointerDown.
  286. /// </summary>
  287. public GameObject pointerPress
  288. {
  289. get { return m_PointerPress; }
  290. set
  291. {
  292. if (m_PointerPress == value)
  293. return;
  294. lastPress = m_PointerPress;
  295. m_PointerPress = value;
  296. }
  297. }
  298. public override string ToString()
  299. {
  300. var sb = new StringBuilder();
  301. sb.AppendLine("<b>Position</b>: " + position);
  302. sb.AppendLine("<b>delta</b>: " + delta);
  303. sb.AppendLine("<b>eligibleForClick</b>: " + eligibleForClick);
  304. sb.AppendLine("<b>pointerEnter</b>: " + pointerEnter);
  305. sb.AppendLine("<b>pointerPress</b>: " + pointerPress);
  306. sb.AppendLine("<b>lastPointerPress</b>: " + lastPress);
  307. sb.AppendLine("<b>pointerDrag</b>: " + pointerDrag);
  308. sb.AppendLine("<b>Use Drag Threshold</b>: " + useDragThreshold);
  309. sb.AppendLine("<b>Current Raycast:</b>");
  310. sb.AppendLine(pointerCurrentRaycast.ToString());
  311. sb.AppendLine("<b>Press Raycast:</b>");
  312. sb.AppendLine(pointerPressRaycast.ToString());
  313. sb.AppendLine("<b>Display Index:</b>");
  314. sb.AppendLine(displayIndex.ToString());
  315. sb.AppendLine("<b>pressure</b>: " + pressure);
  316. sb.AppendLine("<b>tangentialPressure</b>: " + tangentialPressure);
  317. sb.AppendLine("<b>altitudeAngle</b>: " + altitudeAngle);
  318. sb.AppendLine("<b>azimuthAngle</b>: " + azimuthAngle);
  319. sb.AppendLine("<b>twist</b>: " + twist);
  320. sb.AppendLine("<b>tilt</b>: " + tilt);
  321. sb.AppendLine("<b>penStatus</b>: " + penStatus);
  322. sb.AppendLine("<b>radius</b>: " + radius);
  323. sb.AppendLine("<b>radiusVariance</b>: " + radiusVariance);
  324. return sb.ToString();
  325. }
  326. }
  327. }