Brak opisu
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 3.8KB

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