GestureListener

本文介绍了一个自定义的手势识别类MyGestureDetector,该类继承了SimpleOnGestureListener并重写了onFling方法来处理滑动手势。当用户从右向左或从左向右滑动时,该方法能够判断滑动方向,并根据条件改变songInfo变量,实现音乐播放器中歌曲的切换。

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using Windows.Kinect; public class GameManager : MonoBehaviour { [Header("UI References")] public Text timerText; public Text scoreText; public Text currentActionText; public Text progressText; public Text statusText; // 新增:动作状态显示文本 [Header("Game Settings")] public float gameTime = 60f; public int pointsPerAction = 50; public int requiredRepeats = 10; public Animator playerAnimator; private CustomGestureListener gestureListener; private int currentScore = 0; private int actionCount = 0; private int currentPoseID = 0; private string[] poseNames = { "点赞手势(上下摇动)", "双手揮動打招呼", "爱心手势", "跳跃", "胜利姿势扭动" }; // 存储每种动作的达标要求描述 private Dictionary<int, string> actionRequirements = new Dictionary<int, string>() { {1, "大拇指向上的位移 >0.5"}, {2, "两手水平移动距离 >1.0米"}, {3, "手心间距 <0.4米"}, {4, "跳跃高度差 >0.8米"}, {5, "左右手腕高度差 >0.3米"} }; void Start() { gestureListener = FindObjectOfType<CustomGestureListener>(); StartNewGame(); } void Update() { if (gameTime > 0) { gameTime -= Time.deltaTime; timerText.text = $"{Mathf.CeilToInt(gameTime)}"; CheckActionCompletion(); UpdateStatusDisplay(); // 实时更新状态显示 } else { EndGame(); } } void StartNewGame() { currentScore = 0; actionCount = 0; gameTime = 60f; UpdateScoreUI(); UpdateProgressUI(); // 初始化进度显示 SelectRandomPose(); } // 新增:更新进度UI的方法 void UpdateProgressUI() { if (progressText != null) { progressText.text = $"{actionCount}/{requiredRepeats}"; } } // 新增:更新动作状态显示的方法 void UpdateStatusDisplay() { if (statusText == null) return; KinectManager kinectManager = KinectManager.Instance; if (kinectManager == null) return; long userId = kinectManager.GetPrimaryUserID(); if (userId == 0) return; // 获取肢体动作数据 Vector3 spineBasePos = gestureListener.GetJointPosition(userId, JointType.SpineBase); Vector3 headPos = gestureListener.GetJointPosition(userId, JointType.Head); Vector3 leftHandPos = gestureListener.GetJointPosition(userId, JointType.HandLeft); Vector3 rightHandPos = gestureListener.GetJointPosition(userId, JointType.HandRight); Vector3 spineMidPos = gestureListener.GetJointPosition(userId, JointType.SpineMid); float currentValue = 0f; string statusMessage = ""; bool conditionMet = false; switch (currentPoseID) { case 1: // 点赞手势 currentValue = Vector3.Distance(rightHandPos, headPos); conditionMet = gestureListener.IsThumbsUp(rightHandPos); statusMessage = $"状态: {(conditionMet ? "已达成" : "未达成")}\n"; statusMessage += $"要求: {actionRequirements[1]}\n"; statusMessage += $"当前: {currentValue:F1}米"; break; case 2: // 挥手打招呼 currentValue = Vector3.Distance(leftHandPos, rightHandPos); conditionMet = gestureListener.IsWavingHello(leftHandPos, rightHandPos, headPos); statusMessage = $"状态: {(conditionMet ? "已达成" : "未达成")}\n"; statusMessage += $"要求: {actionRequirements[2]}\n"; statusMessage += $"当前: {currentValue:F1}米"; break; case 3: // 爱心手势 currentValue = Vector3.Distance(leftHandPos, rightHandPos); conditionMet = gestureListener.IsHeartShape(leftHandPos, rightHandPos); statusMessage = $"状态: {(conditionMet ? "已达成" : "未达成")}\n"; statusMessage += $"要求: {actionRequirements[3]}\n"; statusMessage += $"当前: {currentValue:F1}米"; break; case 4: // 跳跃 currentValue = spineBasePos.y; conditionMet = gestureListener.IsJumping(spineBasePos); statusMessage = $"状态: {(conditionMet ? "已达成" : "未达成")}\n"; statusMessage += $"要求: {actionRequirements[4]}\n"; statusMessage += $"当前: {currentValue:F1}米"; break; case 5: // 胜利姿势 currentValue = Mathf.Abs(leftHandPos.y - rightHandPos.y); conditionMet = gestureListener.IsVictoryTwist(leftHandPos, rightHandPos, spineMidPos); statusMessage = $"状态: {(conditionMet ? "已达成" : "未达成")}\n"; statusMessage += $"要求: {actionRequirements[5]}\n"; statusMessage += $"当前: {currentValue:F1}米"; break; } statusText.text = statusMessage; // 根据状态改变文本颜色 statusText.color = conditionMet ? Color.green : Color.yellow; } void CheckActionCompletion() { KinectManager kinectManager = KinectManager.Instance; if (kinectManager == null) return; long userId = kinectManager.GetPrimaryUserID(); if (userId == 0) return; Vector3 spineBasePos = gestureListener.GetJointPosition(userId, JointType.SpineBase); Vector3 headPos = gestureListener.GetJointPosition(userId, JointType.Head); Vector3 leftHandPos = gestureListener.GetJointPosition(userId, JointType.HandLeft); Vector3 rightHandPos = gestureListener.GetJointPosition(userId, JointType.HandRight); Vector3 spineMidPos = gestureListener.GetJointPosition(userId, JointType.SpineMid); bool actionComplete = false; switch (currentPoseID) { case 1: actionComplete = gestureListener.IsThumbsUp(rightHandPos); break; case 2: actionComplete = gestureListener.IsWavingHello(leftHandPos, rightHandPos, headPos); break; case 3: actionComplete = gestureListener.IsHeartShape(leftHandPos, rightHandPos); break; case 4: actionComplete = gestureListener.IsJumping(spineBasePos); break; case 5: actionComplete = gestureListener.IsVictoryTwist(leftHandPos, rightHandPos, spineMidPos); break; } if (actionComplete) { actionCount++; UpdateProgressUI(); if (actionCount >= requiredRepeats) { currentScore += pointsPerAction; actionCount = 0; UpdateScoreUI(); UpdateProgressUI(); SelectRandomPose(); } } } // 新增:进度完成时的视觉反馈 IEnumerator ShowCompletionEffect() { if (progressText != null) { progressText.color = Color.green; yield return new WaitForSeconds(0.5f); progressText.color = Color.white; } } void SelectRandomPose() { for (int i = 1; i <= 5; i++) { playerAnimator.SetBool($"pose{i}", false); } int newPose = Random.Range(1, 6); while (newPose == currentPoseID) { newPose = Random.Range(1, 6); } currentPoseID = newPose; playerAnimator.SetBool($"pose{currentPoseID}", true); currentActionText.text = $"当前动作: {poseNames[currentPoseID - 1]}"; } void UpdateScoreUI() { scoreText.text = $"{currentScore}"; } void EndGame() { for (int i = 1; i <= 5; i++) { playerAnimator.SetBool($"pose{i}", false); } timerText.text = "0"; currentActionText.text = $"最终得分: {currentScore}"; } } 根據以上動作變動,改變UpdateStatusDisplay()
最新发布
01-08
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值