GrabPose.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using Rokid.UXR.Utility;
  2. using UnityEngine;
  3. namespace Rokid.UXR.Interaction
  4. {
  5. public class GrabPose : MonoBehaviour
  6. {
  7. [Tooltip("手的类型")]
  8. [SerializeField]
  9. private HandType hand;
  10. private bool trackedSuccess;
  11. private Vector3 offsetToCamera = new Vector3(0, 10000, 0);
  12. private void Start()
  13. {
  14. GesEventInput.OnTrackedSuccess += OnTrackedSuccess;
  15. GesEventInput.OnTrackedFailed += OnTrackedFailed;
  16. trackedSuccess = false;
  17. }
  18. private void OnTrackedSuccess(HandType hand)
  19. {
  20. if (this.hand == hand)
  21. {
  22. trackedSuccess = true;
  23. }
  24. }
  25. private void OnTrackedFailed(HandType handType)
  26. {
  27. if (this.hand == handType || handType == HandType.None)
  28. {
  29. trackedSuccess = false;
  30. }
  31. }
  32. private void OnDestroy()
  33. {
  34. GesEventInput.OnTrackedSuccess -= OnTrackedSuccess;
  35. GesEventInput.OnTrackedFailed -= OnTrackedFailed;
  36. }
  37. private void Update()
  38. {
  39. #if !UNITY_EDITOR
  40. if (trackedSuccess)
  41. {
  42. Pose pose = GesEventInput.Instance.GetHandPose(hand);
  43. transform.SetPose(pose);
  44. }
  45. else
  46. {
  47. this.transform.position = MainCameraCache.mainCamera.transform.position + offsetToCamera;
  48. }
  49. #endif
  50. }
  51. }
  52. }