CanvasRaycastTarget.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal
  10. {
  11. using System.Collections;
  12. using System.Collections.Generic;
  13. using UnityEngine;
  14. using UnityEngine.EventSystems;
  15. using UnityEngine.UI;
  16. /// <summary> Interface for canvas raycast target. </summary>
  17. public interface ICanvasRaycastTarget
  18. {
  19. /// <summary> Gets the canvas. </summary>
  20. /// <value> The canvas. </value>
  21. Canvas canvas { get; }
  22. /// <summary> Gets a value indicating whether this object is enabled. </summary>
  23. /// <value> True if enabled, false if not. </value>
  24. bool enabled { get; }
  25. /// <summary> Gets a value indicating whether the ignore reversed graphics. </summary>
  26. /// <value> True if ignore reversed graphics, false if not. </value>
  27. bool ignoreReversedGraphics { get; }
  28. void GraphicRaycast(bool ignoreReversedGraphics, Ray ray, float distance, Vector3 screenCenterPoint, NRPointerRaycaster raycaster, List<RaycastResult> raycastResults);
  29. }
  30. /// <summary>
  31. /// The class enables an UGUI Canvas and its children to be interactive with NRInput raycasters. </summary>
  32. [RequireComponent(typeof(Canvas))]
  33. [DisallowMultipleComponent]
  34. public class CanvasRaycastTarget : UIBehaviour, ICanvasRaycastTarget
  35. {
  36. /// <summary> The canvas. </summary>
  37. private Canvas m_canvas;
  38. /// <summary> True to ignore reversed graphics. </summary>
  39. [SerializeField]
  40. private bool m_IgnoreReversedGraphics = true;
  41. /// <summary> Gets the canvas. </summary>
  42. /// <value> The canvas. </value>
  43. public virtual Canvas canvas { get { return m_canvas ?? (m_canvas = GetComponent<Canvas>()); } }
  44. /// <summary> Gets or sets a value indicating whether the ignore reversed graphics. </summary>
  45. /// <value> True if ignore reversed graphics, false if not. </value>
  46. public bool ignoreReversedGraphics { get { return m_IgnoreReversedGraphics; } set { m_IgnoreReversedGraphics = value; } }
  47. /// <summary> <para>See MonoBehaviour.OnEnable.</para> </summary>
  48. protected override void OnEnable()
  49. {
  50. base.OnEnable();
  51. CanvasTargetCollector.AddTarget(this);
  52. }
  53. /// <summary> <para>See MonoBehaviour.OnDisable.</para> </summary>
  54. protected override void OnDisable()
  55. {
  56. base.OnDisable();
  57. CanvasTargetCollector.RemoveTarget(this);
  58. }
  59. public virtual void GraphicRaycast(bool ignoreReversedGraphics, Ray ray, float distance, Vector3 screenCenterPoint, NRPointerRaycaster raycaster, List<RaycastResult> raycastResults)
  60. {
  61. var eventCamera = raycaster.eventCamera;
  62. var graphics = GraphicRegistry.GetGraphicsForCanvas(canvas);
  63. for (int i = 0; i < graphics.Count; ++i)
  64. {
  65. var graphic = graphics[i];
  66. // -1 means it hasn't been processed by the canvas, which means it isn't actually drawn
  67. if (graphic.depth == -1 || !graphic.raycastTarget) { continue; }
  68. if (!RectTransformUtility.RectangleContainsScreenPoint(graphic.rectTransform, screenCenterPoint, eventCamera)) { continue; }
  69. if (ignoreReversedGraphics && Vector3.Dot(ray.direction, graphic.transform.forward) <= 0f) { continue; }
  70. if (!graphic.Raycast(screenCenterPoint, eventCamera)) { continue; }
  71. float dist;
  72. new Plane(graphic.transform.forward, graphic.transform.position).Raycast(ray, out dist);
  73. if (dist > distance) { continue; }
  74. var racastResult = new RaycastResult
  75. {
  76. gameObject = graphic.gameObject,
  77. module = raycaster,
  78. distance = dist,
  79. worldPosition = ray.GetPoint(dist),
  80. worldNormal = -graphic.transform.forward,
  81. screenPosition = screenCenterPoint,
  82. index = raycastResults.Count,
  83. depth = graphic.depth,
  84. sortingLayer = canvas.sortingLayerID,
  85. sortingOrder = canvas.sortingOrder
  86. };
  87. raycastResults.Add(racastResult);
  88. }
  89. }
  90. }
  91. }