ItemsCollector.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace NRKernal.NRExamples
  5. {
  6. public class ItemsCollector : MonoBehaviour
  7. {
  8. public NRGrabber rightGrabber;
  9. public NRGrabber leftGrabber;
  10. public Transform[] itemsArr;
  11. private Dictionary<Transform, Pose> m_ItemsPoseDict;
  12. private Dictionary<HandEnum, bool> m_GrabbingStateDict;
  13. private bool m_Inited;
  14. private void Update()
  15. {
  16. if (!m_Inited)
  17. {
  18. Init();
  19. return;
  20. }
  21. UpdateGrabber(HandEnum.RightHand, rightGrabber);
  22. UpdateGrabber(HandEnum.LeftHand, leftGrabber);
  23. }
  24. private void Init()
  25. {
  26. m_ItemsPoseDict = new Dictionary<Transform, Pose>();
  27. m_GrabbingStateDict = new Dictionary<HandEnum, bool>()
  28. {
  29. {HandEnum.RightHand, false},
  30. {HandEnum.LeftHand, false}
  31. };
  32. if (itemsArr != null)
  33. {
  34. for (int i = 0; i < itemsArr.Length; i++)
  35. {
  36. var item = itemsArr[i];
  37. if (item == null)
  38. continue;
  39. m_ItemsPoseDict.Add(item, new Pose(item.position, item.rotation));
  40. }
  41. }
  42. rightGrabber.SetGrabJudgeCondition(() => CheckIsGrabbing(HandEnum.RightHand));
  43. leftGrabber.SetGrabJudgeCondition(() => CheckIsGrabbing(HandEnum.LeftHand));
  44. rightGrabber.gameObject.SetActive(false);
  45. leftGrabber.gameObject.SetActive(false);
  46. m_Inited = true;
  47. }
  48. private void UpdateGrabber(HandEnum handEnum, NRGrabber grabber)
  49. {
  50. if (grabber == null)
  51. return;
  52. if (m_GrabbingStateDict[handEnum] != grabber.IsGrabbingObjects)
  53. {
  54. OnGrabStateChange(handEnum);
  55. m_GrabbingStateDict[handEnum] = grabber.IsGrabbingObjects;
  56. }
  57. var handState = NRInput.Hands.GetHandState(handEnum);
  58. if (handState == null || !handState.isTracked)
  59. {
  60. grabber.gameObject.SetActive(false);
  61. return;
  62. }
  63. grabber.gameObject.SetActive(true);
  64. var grabPose = handState.GetJointPose(HandJointID.IndexProximal);
  65. grabber.transform.position = grabPose.position;
  66. grabber.transform.rotation = grabPose.rotation;
  67. }
  68. private bool CheckIsGrabbing(HandEnum handEnum)
  69. {
  70. var handState = NRInput.Hands.GetHandState(handEnum);
  71. if (handState != null && handState.isTracked && handState.isPinching)
  72. {
  73. return true;
  74. }
  75. return false;
  76. }
  77. private void OnGrabStateChange(HandEnum handEnum)
  78. {
  79. }
  80. public void ResetItems()
  81. {
  82. if (m_ItemsPoseDict != null)
  83. {
  84. foreach (KeyValuePair<Transform, Pose> itemKeyPair in m_ItemsPoseDict)
  85. {
  86. itemKeyPair.Key.position = itemKeyPair.Value.position;
  87. itemKeyPair.Key.rotation = itemKeyPair.Value.rotation;
  88. var rigid = itemKeyPair.Key.GetComponent<Rigidbody>();
  89. if (rigid)
  90. {
  91. rigid.angularVelocity = Vector3.zero;
  92. rigid.velocity = Vector3.zero;
  93. rigid.constraints = RigidbodyConstraints.FreezeAll;
  94. rigid.constraints = RigidbodyConstraints.None;
  95. }
  96. }
  97. }
  98. }
  99. }
  100. }