暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

PhysicsRaycaster.cs 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using System.Collections.Generic;
  2. using UnityEngine.UI;
  3. namespace UnityEngine.EventSystems
  4. {
  5. /// <summary>
  6. /// Simple event system using physics raycasts.
  7. /// </summary>
  8. [AddComponentMenu("Event/Physics Raycaster")]
  9. [RequireComponent(typeof(Camera))]
  10. /// <summary>
  11. /// Raycaster for casting against 3D Physics components.
  12. /// </summary>
  13. public class PhysicsRaycaster : BaseRaycaster
  14. {
  15. /// <summary>
  16. /// Const to use for clarity when no event mask is set
  17. /// </summary>
  18. protected const int kNoEventMaskSet = -1;
  19. protected Camera m_EventCamera;
  20. /// <summary>
  21. /// Layer mask used to filter events. Always combined with the camera's culling mask if a camera is used.
  22. /// </summary>
  23. [SerializeField]
  24. protected LayerMask m_EventMask = kNoEventMaskSet;
  25. /// <summary>
  26. /// The max number of intersections allowed. 0 = allocating version anything else is non alloc.
  27. /// </summary>
  28. [SerializeField]
  29. protected int m_MaxRayIntersections = 0;
  30. protected int m_LastMaxRayIntersections = 0;
  31. #if PACKAGE_PHYSICS
  32. RaycastHit[] m_Hits;
  33. #endif
  34. protected PhysicsRaycaster()
  35. {}
  36. public override Camera eventCamera
  37. {
  38. get
  39. {
  40. if (m_EventCamera == null)
  41. m_EventCamera = GetComponent<Camera>();
  42. return m_EventCamera ?? Camera.main;
  43. }
  44. }
  45. /// <summary>
  46. /// Depth used to determine the order of event processing.
  47. /// </summary>
  48. public virtual int depth
  49. {
  50. get { return (eventCamera != null) ? (int)eventCamera.depth : 0xFFFFFF; }
  51. }
  52. /// <summary>
  53. /// Event mask used to determine which objects will receive events.
  54. /// </summary>
  55. public int finalEventMask
  56. {
  57. get { return (eventCamera != null) ? eventCamera.cullingMask & m_EventMask : kNoEventMaskSet; }
  58. }
  59. /// <summary>
  60. /// Layer mask used to filter events. Always combined with the camera's culling mask if a camera is used.
  61. /// </summary>
  62. public LayerMask eventMask
  63. {
  64. get { return m_EventMask; }
  65. set { m_EventMask = value; }
  66. }
  67. /// <summary>
  68. /// Max number of ray intersection allowed to be found.
  69. /// </summary>
  70. /// <remarks>
  71. /// A value of zero will represent using the allocating version of the raycast function where as any other value will use the non allocating version.
  72. /// </remarks>
  73. public int maxRayIntersections
  74. {
  75. get { return m_MaxRayIntersections; }
  76. set { m_MaxRayIntersections = value; }
  77. }
  78. /// <summary>
  79. /// Returns a ray going from camera through the event position and the distance between the near and far clipping planes along that ray.
  80. /// </summary>
  81. /// <param name="eventData">The pointer event for which we will cast a ray.</param>
  82. /// <param name="ray">The ray to use.</param>
  83. /// <param name="eventDisplayIndex">The display index used.</param>
  84. /// <param name="distanceToClipPlane">The distance between the near and far clipping planes along the ray.</param>
  85. /// <returns>True if the operation was successful. false if it was not possible to compute, such as the eventPosition being outside of the view.</returns>
  86. protected bool ComputeRayAndDistance(PointerEventData eventData, ref Ray ray, ref int eventDisplayIndex, ref float distanceToClipPlane)
  87. {
  88. if (eventCamera == null)
  89. return false;
  90. var eventPosition = MultipleDisplayUtilities.RelativeMouseAtScaled(eventData.position);
  91. if (eventPosition != Vector3.zero)
  92. {
  93. // We support multiple display and display identification based on event position.
  94. eventDisplayIndex = (int)eventPosition.z;
  95. // Discard events that are not part of this display so the user does not interact with multiple displays at once.
  96. if (eventDisplayIndex != eventCamera.targetDisplay)
  97. return false;
  98. }
  99. else
  100. {
  101. // The multiple display system is not supported on all platforms, when it is not supported the returned position
  102. // will be all zeros so when the returned index is 0 we will default to the event data to be safe.
  103. eventPosition = eventData.position;
  104. }
  105. // Cull ray casts that are outside of the view rect. (case 636595)
  106. if (!eventCamera.pixelRect.Contains(eventPosition))
  107. return false;
  108. ray = eventCamera.ScreenPointToRay(eventPosition);
  109. // compensate far plane distance - see MouseEvents.cs
  110. float projectionDirection = ray.direction.z;
  111. distanceToClipPlane = Mathf.Approximately(0.0f, projectionDirection)
  112. ? Mathf.Infinity
  113. : Mathf.Abs((eventCamera.farClipPlane - eventCamera.nearClipPlane) / projectionDirection);
  114. return true;
  115. }
  116. public override void Raycast(PointerEventData eventData, List<RaycastResult> resultAppendList)
  117. {
  118. #if PACKAGE_PHYSICS
  119. Ray ray = new Ray();
  120. int displayIndex = 0;
  121. float distanceToClipPlane = 0;
  122. if (!ComputeRayAndDistance(eventData, ref ray, ref displayIndex, ref distanceToClipPlane))
  123. return;
  124. int hitCount = 0;
  125. if (m_MaxRayIntersections == 0)
  126. {
  127. if (ReflectionMethodsCache.Singleton.raycast3DAll == null)
  128. return;
  129. m_Hits = ReflectionMethodsCache.Singleton.raycast3DAll(ray, distanceToClipPlane, finalEventMask);
  130. hitCount = m_Hits.Length;
  131. }
  132. else
  133. {
  134. if (ReflectionMethodsCache.Singleton.getRaycastNonAlloc == null)
  135. return;
  136. if (m_LastMaxRayIntersections != m_MaxRayIntersections)
  137. {
  138. m_Hits = new RaycastHit[m_MaxRayIntersections];
  139. m_LastMaxRayIntersections = m_MaxRayIntersections;
  140. }
  141. hitCount = ReflectionMethodsCache.Singleton.getRaycastNonAlloc(ray, m_Hits, distanceToClipPlane, finalEventMask);
  142. }
  143. if (hitCount != 0)
  144. {
  145. if (hitCount > 1)
  146. System.Array.Sort(m_Hits, 0, hitCount, RaycastHitComparer.instance);
  147. for (int b = 0, bmax = hitCount; b < bmax; ++b)
  148. {
  149. var result = new RaycastResult
  150. {
  151. gameObject = m_Hits[b].collider.gameObject,
  152. module = this,
  153. distance = m_Hits[b].distance,
  154. worldPosition = m_Hits[b].point,
  155. worldNormal = m_Hits[b].normal,
  156. screenPosition = eventData.position,
  157. displayIndex = displayIndex,
  158. index = resultAppendList.Count,
  159. sortingLayer = 0,
  160. sortingOrder = 0
  161. };
  162. resultAppendList.Add(result);
  163. }
  164. }
  165. #endif
  166. }
  167. #if PACKAGE_PHYSICS
  168. private class RaycastHitComparer : IComparer<RaycastHit>
  169. {
  170. public static RaycastHitComparer instance = new RaycastHitComparer();
  171. public int Compare(RaycastHit x, RaycastHit y)
  172. {
  173. return x.distance.CompareTo(y.distance);
  174. }
  175. }
  176. #endif
  177. }
  178. }