LeapMotionManager.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using UnityEngine;
  2. using Leap;
  3. using Leap.Unity;
  4. using UnityEngine.Events;
  5. using System.Collections;
  6. public class LeapMotionManager : MonoBehaviour
  7. {
  8. public static LeapMotionManager Instance;
  9. public static UnityAction OnHandDetected;
  10. public static UnityAction OnHandLost;
  11. public static UnityAction OnSwipeRight;
  12. public static UnityAction OnSwipeLeft;
  13. // ==================================================
  14. private void Awake() => Instance = this;
  15. private void Update()
  16. {
  17. DetectHands();
  18. DetectSwipe();
  19. }
  20. // ==================================================
  21. #region 检测是否有手
  22. private void DetectHands()
  23. {
  24. Frame frame = Hands.Provider.CurrentFrame;
  25. if (frame.Hands.Count >= 1)
  26. {
  27. Debug.Log(frame.Hands.Count);
  28. if (OnHandDetected != null)
  29. {
  30. OnHandDetected();
  31. }
  32. }
  33. else
  34. {
  35. if (OnHandLost != null)
  36. {
  37. OnHandLost();
  38. }
  39. }
  40. return;
  41. }
  42. #endregion
  43. // ==================================================
  44. #region 设置检测间隔
  45. private float detectSpace = 1f; // 检测间隙
  46. public void SetDetectSpace(float value)
  47. {
  48. detectSpace = value;
  49. return;
  50. }
  51. #endregion
  52. // ==================================================
  53. #region 设置阈值
  54. private float detectThreshold = 0.9f; // 挥手速度的阈值 // 越小越容易检测到
  55. public void SetDetectThreshold(float value)
  56. {
  57. detectThreshold = value;
  58. return;
  59. }
  60. #endregion
  61. // ==================================================
  62. #region 左右挥手检测
  63. private bool allowNotice = true;
  64. private void DetectSwipe()
  65. {
  66. Frame frame = Hands.Provider.CurrentFrame; // 获取当前帧
  67. foreach (Hand hand in frame.Hands) // 检测每只手
  68. {
  69. Vector3 palmVelocity = hand.PalmVelocity; // 获取手的手掌速度
  70. // 检测挥手动作
  71. if (palmVelocity.x > detectThreshold || palmVelocity.x < -detectThreshold)
  72. {
  73. if (!allowNotice) // 是否允许发消息
  74. {
  75. return;
  76. }
  77. allowNotice = false; // 上锁
  78. // 检测到挥手动作,执行相应的逻辑
  79. // Debug.Log("检测到挥手动作,方向:" + (palmVelocity.x > 0 ? "向右" : "向左"));
  80. if (palmVelocity.x > 0)
  81. {
  82. if (OnSwipeRight != null) // 右
  83. {
  84. OnSwipeRight();
  85. }
  86. }
  87. else if (palmVelocity.x < 0) // 左
  88. {
  89. if (OnSwipeLeft != null)
  90. {
  91. OnSwipeLeft();
  92. }
  93. }
  94. StartCoroutine(nameof(BeginCooldown)); // 解锁
  95. }
  96. }
  97. return;
  98. }
  99. private IEnumerator BeginCooldown()
  100. {
  101. yield return new WaitForSeconds(detectSpace);
  102. allowNotice = true;
  103. yield break;
  104. }
  105. #endregion
  106. }