手势的识别步骤:(自己写的继承MonoBehavior和KinectGestures.GestureListenerInterface接口的GestureListener脚本、KinectGestures
脚本、KinectManager脚本)
其中listener脚本要写,其他的赋给场景对象就行。
在listener脚本里的主要操作是实现KinectGestures.GestureListenerInterface接口里的一套流程函(都要public出来给KinectManager调用)
数:
void UserDetected(long userId, int userIndex){ } //玩家识别
void UserLost(long userId, int userIndex){ } //玩家丢失
void GestureInProgress(long userId, int userIndex, KinectGestures.Gestures gesture, float progress,
KinectInterop.JointType joint, Vector3 screenPos){ } //开始手势进度
bool GestureCompleted(long userId, int userIndex, KinectGestures.Gestures gesture,
KinectInterop.JointType joint, Vector3 screenPos){ } //手势完成
bool GestureCancelled(long userId, int userIndex, KinectGestures.Gestures gesture,
KinectInterop.JointType joint){ } //手势销除
在开始手势识别进度时,标志好要识别的手势;
在手势完成或销除时进行相应手势对比和操作;
另外,还要在Update函数里进行识别时间限定,识别时间超过2秒强制消除手势识别。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MyGestureListener : MonoBehaviour, KinectGestures.GestureListenerInterface
{
public GUIText gestureInfo; //状态信息显示Text
private bool progressDisplayed; //进度显示判定
private float progressGestureTime; //手势进度识别时间戳
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(progressDisplayed && (Time.realtimeSinceStartup - progressGestureTime) > 2f)
{
progressDisplayed = false;
if (gestureInfo != null)
{
gestureInfo.text = string.Empty;
}
Debug.Log("手势识别进度超过2秒,强制结束手势识别");
}
}
//玩家识别
public void UserDetected(long userId, int userIndex)
{
KinectManager myManager = KinectManager.Instance;
myManager.DetectGesture(userId, KinectGestures.Gestures.RaiseLeftHand); //标志要检测的手势
myManager.DetectGesture(userId, KinectGestures.Gestures.RaiseRightHand);
myManager.DetectGesture(userId, KinectGestures.Gestures.Psi);
myManager.DetectGesture(userId, KinectGestures.Gestures.Tpose);
myManager.DetectGesture(userId, KinectGestures.Gestures.Stop);
myManager.DetectGesture(userId, KinectGestures.Gestures.Wave);
myManager.DetectGesture(userId, KinectGestures.Gestures.Click);
myManager.DetectGesture(userId, KinectGestures.Gestures.SwipeLeft);
myManager.DetectGesture(userId, KinectGestures.Gestures.SwipeRight);
myManager.DetectGesture(userId, KinectGestures.Gestures.SwipeUp);
myManager.DetectGesture(userId, KinectGestures.Gestures.SwipeDown);
myManager.DetectGesture(userId, KinectGestures.Gestures.ZoomIn);
myManager.DetectGesture(userId, KinectGestures.Gestures.ZoomOut);
myManager.DetectGesture(userId, KinectGestures.Gestures.Wheel);
myManager.DetectGesture(userId, KinectGestures.Gestures.Jump);
myManager.DetectGesture(userId, KinectGestures.Gestures.Squat);
myManager.DetectGesture(userId, KinectGestures.Gestures.Push);
myManager.DetectGesture(userId, KinectGestures.Gestures.Pull);
myManager.DetectGesture(userId, KinectGestures.Gestures.ShoulderLeftFront);
myManager.DetectGesture(userId, KinectGestures.Gestures.ShoulderRightFront);
myManager.DetectGesture(userId, KinectGestures.Gestures.LeanLeft);
myManager.DetectGesture(userId, KinectGestures.Gestures.LeanRight);
myManager.DetectGesture(userId, KinectGestures.Gestures.KickLeft);
myManager.DetectGesture(userId, KinectGestures.Gestures.KickRight);
myManager.DetectGesture(userId, KinectGestures.Gestures.Run);
if (gestureInfo != null)
{
gestureInfo.text = "已开始识别手势";
}
Debug.Log("发现用户");
}
//玩家丢失
public void UserLost(long userId, int userIndex)
{
if (gestureInfo != null)
{
gestureInfo.text = string.Empty;
}
Debug.Log("丢失用户");
}
//开始手势进度
public void GestureInProgress(long userId, int userIndex, KinectGestures.Gestures gesture, float progress,
KinectInterop.JointType joint, Vector3 screenPos)
{
//手势进度超过50%才重新判断,避免相似手势之间混淆
if ((gesture == KinectGestures.Gestures.ZoomIn || gesture == KinectGestures.Gestures.ZoomOut) && progress > 0.5f)
{
if (gestureInfo != null)
{
string sGestureText = string.Format("{0}-progress:{1}%", gesture, screenPos.z * 100f);
gestureInfo.text = sGestureText;
progressDisplayed = true;
progressGestureTime = Time.realtimeSinceStartup;
}
}
else if ((gesture == KinectGestures.Gestures.Wheel || gesture == KinectGestures.Gestures.LeanLeft ||
gesture == KinectGestures.Gestures.LeanRight) && progress > 0.5f)
{
if (gestureInfo != null)
{
string sGestureText = string.Format("{0}-progress:{1}%", gesture, screenPos.z * 100f);
gestureInfo.text = sGestureText;
progressDisplayed = true;
progressGestureTime = Time.realtimeSinceStartup;
}
}
else if (gesture == KinectGestures.Gestures.Run && progress > 0.5f)
{
if (gestureInfo != null)
{
string sGestureText = string.Format("{0}-progress:{1}%", gesture, screenPos.z * 100f);
gestureInfo.text = sGestureText;
progressDisplayed = true;
progressGestureTime = Time.realtimeSinceStartup;
}
}
}
//手势完成
public bool GestureCompleted(long userId, int userIndex, KinectGestures.Gestures gesture,
KinectInterop.JointType joint, Vector3 screenPos)
{
if (progressDisplayed) { return true; }
string sGestureText = gesture + "完成";
if (gestureInfo != null)
{
gestureInfo.text = sGestureText;
}
//手势识别完成后的操作
if (gesture == KinectGestures.Gestures.RaiseLeftHand) { Debug.Log("太好了," + gesture + "手势成功识别"); }
if (gesture ==KinectGestures.Gestures.RaiseRightHand) { Debug.Log("太好了," + gesture + "手势成功识别"); }
if (gesture ==KinectGestures.Gestures.Psi) { Debug.Log("太好了," + gesture + "手势成功识别"); }
if (gesture ==KinectGestures.Gestures.Tpose) { Debug.Log("太好了," + gesture + "手势成功识别"); }
if (gesture ==KinectGestures.Gestures.Stop) { Debug.Log("太好了," + gesture + "手势成功识别"); }
if (gesture ==KinectGestures.Gestures.Wave) { Debug.Log("太好了," + gesture + "手势成功识别"); }
if (gesture ==KinectGestures.Gestures.Click) { Debug.Log("太好了," + gesture + "手势成功识别"); }
if (gesture ==KinectGestures.Gestures.SwipeLeft) { Debug.Log("太好了," + gesture + "手势成功识别"); }
if (gesture ==KinectGestures.Gestures.SwipeRight) { Debug.Log("太好了," + gesture + "手势成功识别"); }
if (gesture ==KinectGestures.Gestures.SwipeUp) { Debug.Log("太好了," + gesture + "手势成功识别"); }
if (gesture ==KinectGestures.Gestures.SwipeDown) { Debug.Log("太好了," + gesture + "手势成功识别"); }
if (gesture ==KinectGestures.Gestures.ZoomIn) { Debug.Log("太好了," + gesture + "手势成功识别"); }
if (gesture ==KinectGestures.Gestures.ZoomOut) { Debug.Log("太好了," + gesture + "手势成功识别"); }
if (gesture ==KinectGestures.Gestures.Wheel) { Debug.Log("太好了," + gesture + "手势成功识别"); }
if (gesture ==KinectGestures.Gestures.Jump) { Debug.Log("太好了," + gesture + "手势成功识别"); }
if (gesture ==KinectGestures.Gestures.Squat) { Debug.Log("太好了," + gesture + "手势成功识别"); }
if (gesture ==KinectGestures.Gestures.Push) { Debug.Log("太好了," + gesture + "手势成功识别"); }
if (gesture ==KinectGestures.Gestures.Pull) { Debug.Log("太好了," + gesture + "手势成功识别"); }
if (gesture ==KinectGestures.Gestures.ShoulderLeftFront) { Debug.Log("太好了," + gesture + "手势成功识别"); }
if (gesture ==KinectGestures.Gestures.ShoulderRightFront) { Debug.Log("太好了," + gesture + "手势成功识别"); }
if (gesture ==KinectGestures.Gestures.LeanLeft) { Debug.Log("太好了," + gesture + "手势成功识别"); }
if (gesture ==KinectGestures.Gestures.LeanRight) { Debug.Log("太好了," + gesture + "手势成功识别"); }
if (gesture ==KinectGestures.Gestures.KickLeft) { Debug.Log("太好了," + gesture + "手势成功识别"); }
if (gesture ==KinectGestures.Gestures.KickRight) { Debug.Log("太好了," + gesture + "手势成功识别"); }
if (gesture == KinectGestures.Gestures.Run) { Debug.Log("太好了," + gesture + "手势成功识别"); }
return false;
}
//手势销除
public bool GestureCancelled(long userId, int userIndex, KinectGestures.Gestures gesture,
KinectInterop.JointType joint)
{
if (progressDisplayed)
{
progressDisplayed = false;
if (gestureInfo != null)
{
gestureInfo.text = string.Empty;
}
}
//手势销除时进行操作
return false;
}
}