MouseRayPose.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using UnityEngine;
  2. using Rokid.UXR.Native;
  3. using Rokid.UXR.Utility;
  4. namespace Rokid.UXR.Interaction
  5. {
  6. public class MouseRayPose : BaseRayPose
  7. {
  8. [SerializeField, Tooltip("移动速度")]
  9. private float moveSpeed = 1.0f;
  10. private float yaw;
  11. private float pitch;
  12. private float roll;
  13. private void Awake()
  14. {
  15. ResetPose();
  16. MouseEventInput.OnActiveMouseModule += OnActiveMouseModule;
  17. renderCamera = Camera.main;
  18. }
  19. private void OnDestroy()
  20. {
  21. MouseEventInput.OnActiveMouseModule -= OnActiveMouseModule;
  22. }
  23. private void OnEnable()
  24. {
  25. MouseEventInput.OnMouseMove += OnMouseMove;
  26. }
  27. private void OnDisable()
  28. {
  29. MouseEventInput.OnMouseMove -= OnMouseMove;
  30. }
  31. private void OnActiveMouseModule()
  32. {
  33. ResetPose();
  34. }
  35. /// <summary>
  36. /// 重置射线位姿
  37. /// </summary>
  38. private void ResetPose()
  39. {
  40. RKLog.Debug("====MouseRayPose====: Reset Pose");
  41. transform.position = MainCameraCache.mainCamera.transform.position;
  42. transform.rotation = MainCameraCache.mainCamera.transform.rotation;
  43. }
  44. private void OnMouseMove(Vector2 delta)
  45. {
  46. if (Utils.IsUnityEditor())
  47. return;
  48. RKLog.Debug("====MouseRayPose====:" + delta);
  49. pitch = transform.rotation.eulerAngles.x;
  50. yaw = transform.rotation.eulerAngles.y;
  51. roll = transform.rotation.eulerAngles.z;
  52. #if UNITY_EDITOR
  53. moveSpeed = 1;
  54. pitch += -delta.y * moveSpeed;
  55. #else
  56. pitch += delta.y * moveSpeed;
  57. #endif
  58. yaw += delta.x * moveSpeed;
  59. transform.rotation = Quaternion.Euler(pitch, yaw, 0);
  60. LimitInViewField();
  61. }
  62. #if UNITY_EDITOR
  63. protected override void Update()
  64. {
  65. base.Update();
  66. LimitInViewField();
  67. }
  68. #endif
  69. }
  70. }