MouseCursorVisual.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Rokid.UXR;
  5. using Rokid.UXR.Utility;
  6. using UnityEngine.EventSystems;
  7. namespace Rokid.UXR.Interaction
  8. {
  9. public class MouseCursorVisual : MonoBehaviour
  10. {
  11. [SerializeField]
  12. private RayInteractor rayInteractor;
  13. [Tooltip("光标Size")]
  14. [SerializeField]
  15. private float cursorAngularSize = 100;
  16. [SerializeField]
  17. private Transform focusCursor;
  18. [SerializeField]
  19. private Transform pressCursor;
  20. private void Start()
  21. {
  22. rayInteractor.WhenPostprocessed += UpdateVisual;
  23. rayInteractor.WhenStateChanged += UpdateVisualState;
  24. }
  25. private void OnDestroy()
  26. {
  27. rayInteractor.WhenPostprocessed -= UpdateVisual;
  28. rayInteractor.WhenStateChanged -= UpdateVisualState;
  29. }
  30. private void OnEnable()
  31. {
  32. RKPointerLisener.OnGraphicPointerEnter += OnPointerEnter;
  33. RKPointerLisener.OnGraphicPointerExit += OnPointerExit;
  34. RKPointerLisener.OnGraphicPointerHover += OnPointerHover;
  35. }
  36. private void OnDisable()
  37. {
  38. RKPointerLisener.OnGraphicPointerEnter -= OnPointerEnter;
  39. RKPointerLisener.OnGraphicPointerExit -= OnPointerExit;
  40. RKPointerLisener.OnGraphicPointerHover -= OnPointerHover;
  41. IsInHover = false;
  42. }
  43. private Vector3 ComputeScaleWithAngularScale(Vector3 targetPoint)
  44. {
  45. float cursorDistance = Vector3.Distance(MainCameraCache.mainCamera.transform.position, targetPoint);
  46. float desiredScale = Utils.ScaleFromAngularSizeAndDistance(cursorAngularSize, cursorDistance);
  47. return Vector3.one * desiredScale;
  48. }
  49. private void UpdateVisual()
  50. {
  51. if (rayInteractor.State == InteractorState.Disabled)
  52. {
  53. gameObject.SetActive(false);
  54. return;
  55. }
  56. gameObject.SetActive(true);
  57. Vector3 EndPosition;
  58. Vector3 EndNormal;
  59. if (IsInHover && HoverNormal != Vector3.zero && HoverPosition != Vector3.zero)
  60. {
  61. EndPosition = HoverPosition;
  62. EndNormal = HoverNormal;
  63. }
  64. else
  65. {
  66. EndPosition = rayInteractor.End;
  67. EndNormal = rayInteractor.Forward;
  68. }
  69. this.transform.position = EndPosition - EndNormal * 0.001f;
  70. if (rayInteractor.State == InteractorState.Select || rayInteractor.State == InteractorState.Hover)
  71. {
  72. if (rayInteractor.CollisionInfo != null)
  73. {
  74. Quaternion rotation = Quaternion.FromToRotation(Vector3.forward, -rayInteractor.CollisionInfo.Value.Normal);
  75. this.transform.eulerAngles = new Vector3(rotation.eulerAngles.x, rotation.eulerAngles.y, 0);
  76. }
  77. else
  78. {
  79. this.transform.rotation = Quaternion.LookRotation(EndPosition - MainCameraCache.mainCamera.transform.position, Vector3.up);
  80. }
  81. this.transform.localScale = ComputeScaleWithAngularScale(EndPosition);
  82. }
  83. else if (rayInteractor.State == InteractorState.Normal)
  84. {
  85. this.transform.rotation = Quaternion.LookRotation(EndPosition - MainCameraCache.mainCamera.transform.position, Vector3.up);
  86. this.transform.localScale = ComputeScaleWithAngularScale(EndPosition);
  87. }
  88. bool press = rayInteractor.State == InteractorState.Select;
  89. focusCursor.gameObject.SetActive(!press);
  90. pressCursor.gameObject.SetActive(press);
  91. }
  92. private void UpdateVisualState(InteractorStateChangeArgs args) => UpdateVisual();
  93. #region ForCursorHoverPose
  94. private bool IsInHover;
  95. private Vector3 HoverPosition;
  96. private Vector3 HoverNormal;
  97. private void OnPointerEnter(PointerEventData eventData)
  98. {
  99. if (eventData.pointerCurrentRaycast.gameObject != null)
  100. {
  101. IsInHover = true;
  102. HoverPosition = Vector3.zero;
  103. HoverNormal = Vector3.zero;
  104. }
  105. }
  106. private void OnPointerExit(PointerEventData eventData)
  107. {
  108. IsInHover = false;
  109. HoverPosition = Vector3.zero;
  110. HoverNormal = Vector3.zero;
  111. }
  112. private void OnPointerHover(PointerEventData eventData)
  113. {
  114. HoverPosition = eventData.pointerCurrentRaycast.worldPosition;
  115. HoverNormal = eventData.pointerCurrentRaycast.worldNormal;
  116. }
  117. #endregion
  118. }
  119. }