Unity + Kinect手势捕捉

手势捕捉只需要三个脚本Kinectmanager和KinectGestures是SDK自带的脚本,Gesture_ZR是我自己写的,里边可以实现你的逻辑

Unity + Kinect手势捕捉_第1张图片

using UnityEngine;
using System.Collections;
using System;
//using Windows.Kinect;

public class Gesture_ZR : MonoBehaviour, KinectGestures.GestureListenerInterface
{
    [Tooltip("播放器的索引,由该组件跟踪。0表示第一个玩家,1 - 2,2 - 3,等等。")]
    public int playerIndex = 0;
     ///


    /// 当检测到新用户时调用。在这里,您可以通过调用KinectManager.DetectGesture()-函数来启动手势跟踪。
    ///

    public void UserDetected(long userId, int userIndex)
    {
        // 这些手势只允许主要用户使用
        KinectManager manager = KinectManager.Instance;
        if (!manager || (userIndex != playerIndex))
            return;
        // 检测这些用户特定的手势
        manager.DetectGesture(userId, KinectGestures.Gestures.SwipeLeft);
        manager.DetectGesture(userId, KinectGestures.Gestures.SwipeRight);
        manager.DetectGesture(userId, KinectGestures.Gestures.SwipeUp);
        manager.DetectGesture(userId, KinectGestures.Gestures.RaiseRightHand);
    }
    ///
    /// 当用户丢失时调用。自动清除此用户的所有跟踪手势。
    ///

    /// User ID
    /// User index
    public void UserLost(long userId, int userIndex)
    {
        // 这些手势只允许主要用户使用
        if (userIndex != playerIndex)
            return;
    }
    ///
    /// 在进行手势时调用。
    ///

    /// User ID
    /// User index
    /// Gesture type
    /// Gesture progress [0..1]
    /// Joint type
    /// Normalized viewport position
    public void GestureInProgress(long userId, int userIndex, KinectGestures.Gestures gesture,
                                  float progress, KinectInterop.JointType joint, Vector3 screenPos)
    {
        // 这些手势只允许主要用户使用
        if (userIndex != playerIndex)
            return;
    }
    ///
    ///在手势完成时调用。
    ///

    /// true
    /// false
    /// User ID
    /// User index
    /// Gesture type
    /// Joint type
    /// Normalized viewport position
    public bool GestureCompleted(long userId, int userIndex, KinectGestures.Gestures gesture,
                                  KinectInterop.JointType joint, Vector3 screenPos)
    {
        // the gestures are allowed for the primary user only
        if (userIndex != playerIndex)
            return false;
        //向左滑动
        if (gesture == KinectGestures.Gestures.SwipeLeft)
        {
            Debug.Log("向左滑动后的逻辑");
        }
        //向右滑动
        else if (gesture == KinectGestures.Gestures.SwipeRight)
        {
            Debug.Log("向右滑动后的逻辑");
        }
        else if (gesture == KinectGestures.Gestures.RaiseRightHand)
        {
            PhotoShooter photoShooter = gameObject.GetComponent();
            if (photoShooter && photoShooter.enabled)
            {
                photoShooter.CountdownAndMakePhoto();
            }
        }

            return true;
    }

    ///


    ///如果取消某个手势,则调用。
    ///

    /// true
    /// false
    /// User ID
    /// User index
    /// Gesture type
    /// Joint type
    public bool GestureCancelled(long userId, int userIndex, KinectGestures.Gestures gesture,
                                  KinectInterop.JointType joint)
    {
        // the gestures are allowed for the primary user only
        if (userIndex != playerIndex)
        { 
            return false;
        }
        else { return true;}
    }
}
 

以下手势能被流畅的识别:

◎RaiseRightHand / RaiseLeftHand – 左手或右手举起过肩并保持至少一秒

◎Psi –双手举起过肩并保持至少一秒

◎Stop – 双手下垂.

◎Wave –左手或右手举起来回摆动

◎SwipeLeft – 右手向左挥.

◎SwipeRight – 左手向右挥.

◎SwipeUp / SwipeDown – 左手或者右手向上/下挥

◎ Click – 左手或右手在适当的位置停留至少2.5秒.

◎RightHandCursor / LeftHandCursor – 假手势,用来使光标随着手移动

◎ZoomOut – 手肘向下,左右手掌合在一起(求佛的手势),然后慢慢分开.

◎ZoomIn – 手肘向下,两手掌相聚至少0.7米,然后慢慢合在一起

◎Wheel –英文原版描述不清,就是ZoomOut/In的手势,只不过在动的时候是前后而非左右。

◎Jump –在1.5秒内髋关节中心至少上升10厘米

◎Squat -在1.5秒内髋关节中心至少下降10厘米

◎Push – 在1.5秒内将左手或右手向外推

◎Pull -在1.5秒内将左手或右手向里拉
 

你可能感兴趣的:(Unity + Kinect手势捕捉)