NxrLineOfSight.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.EventSystems;
  4. using UnityEngine.UI;
  5. namespace Nxr.Internal
  6. {
  7. public class NxrLineOfSight : MonoBehaviour
  8. {
  9. internal delegate void NonLookAction();
  10. internal static event NonLookAction NonLook;
  11. private Transform _myTransform;
  12. public GameObject lineOfSightDot;
  13. public Canvas canvas;
  14. public bool drawDebugRay;
  15. // Use this for initialization
  16. void Start()
  17. {
  18. // dismiss original reticle or dtr point
  19. NxrViewer.Instance.SwitchControllerMode(true);
  20. _myTransform = GetComponentInParent<Transform>();
  21. if(_myTransform == null)
  22. {
  23. Debug.Log("_myTransform is NULL !!!");
  24. }
  25. if (lineOfSightDot != null)
  26. {
  27. lineOfSightDot = Instantiate(lineOfSightDot);
  28. lineOfSightDot.gameObject.SetActive(true);
  29. }
  30. }
  31. // Update is called once per frame
  32. void Update()
  33. {
  34. RaycastHit hitInfo;
  35. // _myTransform.position = new Vector3(0, 0, 0);
  36. //this.MyTransform.rotation
  37. Vector3 vector = _myTransform.TransformDirection(Vector3.forward);
  38. Ray ray = new Ray(_myTransform.position, vector);
  39. if(drawDebugRay) UnityEngine.Debug.DrawRay(ray.origin, ray.direction * 250f, Color.cyan);
  40. if (Physics.Raycast(ray, out hitInfo))
  41. {
  42. if (this.lineOfSightDot != null)
  43. {
  44. this.lineOfSightDot.SetActive(true);
  45. this.lineOfSightDot.transform.position = hitInfo.point - hitInfo.normal * 0.1f;
  46. this.lineOfSightDot.transform.localRotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
  47. }
  48. if (!this.InformObject(hitInfo.transform.gameObject))
  49. {
  50. TriggerNonLook();
  51. }
  52. }
  53. else if (canvas != null && IsPointerOverUIObject(canvas, new Vector2(Screen.width / 2, Screen.height / 2)))
  54. {
  55. RaycastResult raycastResult = results.Count >0 ? results[0] : new RaycastResult();
  56. if(raycastResult.gameObject != null)
  57. {
  58. // Debug.Log("===UI=>" + raycastResult.distance + "," + GetIntersectionPosition().ToString("f4"));
  59. if (this.lineOfSightDot != null)
  60. {
  61. this.lineOfSightDot.SetActive(true);
  62. this.lineOfSightDot.transform.position = GetIntersectionPosition() + new Vector3(0,0,-0.2f);
  63. this.lineOfSightDot.transform.localRotation = Quaternion.Euler(-90,0,0);
  64. }
  65. if (!this.InformObject(raycastResult.gameObject))
  66. {
  67. TriggerNonLook();
  68. }
  69. }
  70. }
  71. else
  72. {
  73. if (this.lineOfSightDot != null)
  74. {
  75. this.lineOfSightDot.SetActive(false);
  76. }
  77. TriggerNonLook();
  78. }
  79. }
  80. List<RaycastResult> results = new List<RaycastResult>();
  81. PointerEventData eventDataCurrentPosition;
  82. GraphicRaycaster uiRaycaster;
  83. // 通过画布上的 GraphicRaycaster 组件发射射线
  84. public bool IsPointerOverUIObject(Canvas canvas, Vector2 screenPosition)
  85. {
  86. //实例化点击事件
  87. if (eventDataCurrentPosition == null)
  88. {
  89. eventDataCurrentPosition = new PointerEventData(EventSystem.current);
  90. }
  91. else
  92. {
  93. eventDataCurrentPosition.Reset();
  94. }
  95. //将点击位置的屏幕坐标赋值给点击事件
  96. eventDataCurrentPosition.position = screenPosition;
  97. //获取画布上的 GraphicRaycaster 组件
  98. if (uiRaycaster == null)
  99. {
  100. uiRaycaster = canvas.gameObject.GetComponent<GraphicRaycaster>();
  101. }
  102. results.Clear();
  103. // GraphicRaycaster 发射射线
  104. uiRaycaster.Raycast(eventDataCurrentPosition, results);
  105. if(results.Count > 0) eventDataCurrentPosition.pointerCurrentRaycast = results[0];
  106. return results.Count > 0;
  107. }
  108. Vector3 GetIntersectionPosition()
  109. {
  110. // Check for camera
  111. Camera cam = eventDataCurrentPosition.enterEventCamera;
  112. if (cam == null)
  113. {
  114. return Vector3.zero;
  115. }
  116. float intersectionDistance = eventDataCurrentPosition.pointerCurrentRaycast.distance + cam.nearClipPlane;
  117. Vector3 intersectionPosition = cam.transform.position + cam.transform.forward * intersectionDistance;
  118. return intersectionPosition;
  119. }
  120. private NxrInteractive currentObject;
  121. private bool InformObject(GameObject go)
  122. {
  123. NxrInteractive component = go.GetComponent<NxrInteractive>();
  124. if (component == null)
  125. {
  126. return false;
  127. }
  128. component.HandleIsLookedAt();
  129. if (this.currentObject != component)
  130. {
  131. if (this.currentObject == null)
  132. {
  133. this.currentObject = null;
  134. }
  135. else
  136. {
  137. this.currentObject.OtherIsLooked();
  138. }
  139. this.currentObject = component;
  140. }
  141. return true;
  142. }
  143. private static void TriggerNonLook()
  144. {
  145. if (NonLook != null)
  146. {
  147. NonLook();
  148. }
  149. }
  150. }
  151. }