RKWatchDemo.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using UnityEngine.UI;
  2. using UnityEngine;
  3. namespace Rokid.UXR.Interaction
  4. {
  5. public class RKWatchDemo : MonoBehaviour
  6. {
  7. [SerializeField]
  8. private HandType hand;
  9. [SerializeField]
  10. private Text logText;
  11. private Color[] watchColors = new Color[] { Color.red, Color.green, Color.blue };
  12. private int colorIndex = 0;
  13. private Vector3 oriScale;
  14. private Material watchMat;
  15. private bool active;
  16. private bool stateChange;
  17. void Awake()
  18. {
  19. RKHandWatch.OnActiveWatch += OnActiveWatch;
  20. RKHandWatch.OnWatchPoseUpdate += OnWatchPoseUpdate;
  21. }
  22. private void Start()
  23. {
  24. this.gameObject.SetActive(false);
  25. watchMat = GetComponent<MeshRenderer>()?.material;
  26. oriScale = transform.localScale;
  27. }
  28. private void OnDestroy()
  29. {
  30. RKHandWatch.OnActiveWatch -= OnActiveWatch;
  31. RKHandWatch.OnWatchPoseUpdate -= OnWatchPoseUpdate;
  32. }
  33. private void OnDisable()
  34. {
  35. colorIndex = 0;
  36. watchMat?.SetColor("_Color", watchColors[colorIndex]);
  37. }
  38. private void OnWatchPoseUpdate(HandType hand, Pose pose)
  39. {
  40. if (hand == this.hand)
  41. transform.SetPose(pose);
  42. }
  43. private void OnActiveWatch(HandType hand, bool active)
  44. {
  45. if (hand == this.hand)
  46. {
  47. if (this.active != active && active == true)
  48. {
  49. stateChange = true;
  50. }
  51. this.active = active;
  52. this.gameObject.SetActive(active);
  53. }
  54. }
  55. private Pose GetSkeletonPose(SkeletonIndexFlag index, HandType hand)
  56. {
  57. return GesEventInput.Instance.GetSkeletonPose(index, hand);
  58. }
  59. private void Update()
  60. {
  61. if (active == true && stateChange == true)
  62. {
  63. stateChange = false;
  64. return;
  65. }
  66. if (GesEventInput.Instance.GetHandDown(hand, false))
  67. {
  68. colorIndex++;
  69. if (colorIndex == watchColors.Length)
  70. {
  71. colorIndex = 0;
  72. }
  73. // 处理切换逻辑
  74. watchMat.SetColor("_Color", watchColors[colorIndex]);
  75. }
  76. }
  77. }
  78. }