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.

Physics2DRaycaster.cs 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System.Collections.Generic;
  2. using UnityEngine.UI;
  3. using UnityEngine.Rendering;
  4. #if PACKAGE_TILEMAP
  5. using UnityEngine.Tilemaps;
  6. #endif
  7. using UnityEngine.U2D;
  8. namespace UnityEngine.EventSystems
  9. {
  10. /// <summary>
  11. /// Simple event system using physics raycasts.
  12. /// </summary>
  13. [AddComponentMenu("Event/Physics 2D Raycaster")]
  14. [RequireComponent(typeof(Camera))]
  15. /// <summary>
  16. /// Raycaster for casting against 2D Physics components.
  17. /// </summary>
  18. public class Physics2DRaycaster : PhysicsRaycaster
  19. {
  20. #if PACKAGE_PHYSICS2D
  21. RaycastHit2D[] m_Hits;
  22. #endif
  23. protected Physics2DRaycaster()
  24. {}
  25. /// <summary>
  26. /// Raycast against 2D elements in the scene.
  27. /// </summary>
  28. public override void Raycast(PointerEventData eventData, List<RaycastResult> resultAppendList)
  29. {
  30. #if PACKAGE_PHYSICS2D
  31. Ray ray = new Ray();
  32. float distanceToClipPlane = 0;
  33. int displayIndex = 0;
  34. if (!ComputeRayAndDistance(eventData, ref ray, ref displayIndex, ref distanceToClipPlane))
  35. return;
  36. int hitCount = 0;
  37. if (maxRayIntersections == 0)
  38. {
  39. if (ReflectionMethodsCache.Singleton.getRayIntersectionAll == null)
  40. return;
  41. m_Hits = ReflectionMethodsCache.Singleton.getRayIntersectionAll(ray, distanceToClipPlane, finalEventMask);
  42. hitCount = m_Hits.Length;
  43. }
  44. else
  45. {
  46. if (ReflectionMethodsCache.Singleton.getRayIntersectionAllNonAlloc == null)
  47. return;
  48. if (m_LastMaxRayIntersections != m_MaxRayIntersections)
  49. {
  50. m_Hits = new RaycastHit2D[maxRayIntersections];
  51. m_LastMaxRayIntersections = m_MaxRayIntersections;
  52. }
  53. hitCount = ReflectionMethodsCache.Singleton.getRayIntersectionAllNonAlloc(ray, m_Hits, distanceToClipPlane, finalEventMask);
  54. }
  55. if (hitCount != 0)
  56. {
  57. for (int b = 0, bmax = hitCount; b < bmax; ++b)
  58. {
  59. Renderer r2d = null;
  60. // Case 1198442: Check for 2D renderers when filling in RaycastResults
  61. var rendererResult = m_Hits[b].collider.gameObject.GetComponent<Renderer>();
  62. if (rendererResult != null)
  63. {
  64. if (rendererResult is SpriteRenderer)
  65. {
  66. r2d = rendererResult;
  67. }
  68. #if PACKAGE_TILEMAP
  69. if (rendererResult is TilemapRenderer)
  70. {
  71. r2d = rendererResult;
  72. }
  73. #endif
  74. if (rendererResult is SpriteShapeRenderer)
  75. {
  76. r2d = rendererResult;
  77. }
  78. }
  79. var result = new RaycastResult
  80. {
  81. gameObject = m_Hits[b].collider.gameObject,
  82. module = this,
  83. distance = m_Hits[b].distance,
  84. worldPosition = m_Hits[b].point,
  85. worldNormal = m_Hits[b].normal,
  86. screenPosition = eventData.position,
  87. displayIndex = displayIndex,
  88. index = resultAppendList.Count,
  89. sortingGroupID = r2d != null ? r2d.sortingGroupID : SortingGroup.invalidSortingGroupID,
  90. sortingGroupOrder = r2d != null ? r2d.sortingGroupOrder : 0,
  91. sortingLayer = r2d != null ? r2d.sortingLayerID : 0,
  92. sortingOrder = r2d != null ? r2d.sortingOrder : 0
  93. };
  94. if (result.sortingGroupID != SortingGroup.invalidSortingGroupID &&
  95. SortingGroup.GetSortingGroupByIndex(r2d.sortingGroupID) is SortingGroup sortingGroup)
  96. {
  97. // Calculate how far along the ray the sorting group is.
  98. result.distance = Vector3.Dot(ray.direction, sortingGroup.transform.position - ray.origin);
  99. result.sortingLayer = sortingGroup.sortingLayerID;
  100. result.sortingOrder = sortingGroup.sortingOrder;
  101. }
  102. resultAppendList.Add(result);
  103. }
  104. }
  105. #endif
  106. }
  107. }
  108. }