栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > 其他

Unity Kinect - 记录基本用法 - 左右挥手

其他 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Unity Kinect - 记录基本用法 - 左右挥手

        新建GestureListener脚本,继承 KinectGestures.GestureListenerInterface 接口。

public class GestureListener : MonoBehaviour, KinectGestures.GestureListenerInterface{}

        继承接口后要实现接口内的所有内容,按提示实现接口,共五个。

// 检测到用户时
public void UserDetected(long userId, int userIndex){}

// 丢失用户时
public void UserLost(long userId, int userIndex){}

// 检测到手势正在进行
public void GestureInProgress(long userId, int userIndex, KinectGestures.Gestures gesture, float progress, KinectInterop.JointType joint, Vector3 screenPos){}

// 手势完成
public bool GestureCompleted(long userId, int userIndex, KinectGestures.Gestures gesture, KinectInterop.JointType joint, Vector3 screenPos){}

// 手势没有完成
public bool GestureCancelled(long userId, int userIndex, KinectGestures.Gestures gesture, KinectInterop.JointType joint){}

        通常在以上函数内需要判断是否连接到Kinect和当前用户

// 声明一个int型变量表明第几个用户
private int playerIndex = 0;

// 每个接口内最好都判断
public void UserDetected(long userId, int userIndex)
{
    KinectManager manager = KinectManager.Instance;

    if (!manager || (userIndex != playerIndex))
        return;
}       

        具体使用:

public class GestureListener : MonoBehaviour, KinectGestures.GestureListenerInterface
{
    private static GestureListener instance = null;

    // 右手往左挥
    private bool swipeLeft;
    
    // 左手往右挥
    private bool swipeRight;

    private int playerIndex = 0;

    void Awake()
    {
        instance = this;
    }

    public static GestureListener Instance
    {
        get
        {
            return instance;
        }
    }
    
    public bool IsSwipeLeft()
    {
        if (swipeLeft)
        {
            swipeLeft = false;

            return true;
        }

        return false;
    }

    public bool IsSwipeRight()
    {
        if (swipeRight)
        {
            swipeRight = false;

            return true;
        }

        return false;
    }
    // 识别到第一个用户后,在用户手势列表中增加两个挥手手势
    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);
    }
    // 第一个用户丢失后,移除手势列表里的手势
    public void UserLost(long userId, int userIndex)
    {
        KinectManager manager = KinectManager.Instance;

        if (!manager || (userIndex != playerIndex))
            return;

        manager.DeleteGesture(userId, KinectGestures.Gestures.SwipeLeft);

        manager.DeleteGesture(userId, KinectGestures.Gestures.SwipeRight);
    }
    // 手势进行中时,progress展示完成度
    public void GestureInProgress(long userId, int userIndex, KinectGestures.Gestures gesture, float progress, KinectInterop.JointType joint, Vector3 screenPos)
    {
        KinectManager manager = KinectManager.Instance;

        if (!manager || (userIndex != playerIndex))
            return;
    }
    // 手势完成后
    public bool GestureCompleted(long userId, int userIndex, KinectGestures.Gestures gesture, KinectInterop.JointType joint, Vector3 screenPos)
    {
        KinectManager manager = KinectManager.Instance;

        if (!manager || (userIndex != playerIndex))
            return false;
        
        // 左挥完成后,swipeLeft为true
        if (gesture == KinectGestures.Gestures.SwipeLeft)
        {
            swipeLeft = true;
        }
            
        else if (gesture == KinectGestures.Gestures.SwipeRight)
        {
            swipeRight = true;
        }
           
        return true;
    }
    
    // 手势没做完
    public bool GestureCancelled(long userId, int userIndex, KinectGestures.Gestures gesture, KinectInterop.JointType joint)
    {
        KinectManager manager = KinectManager.Instance;

        if (!manager || (userIndex != playerIndex))
            return false;

        return true;
    }

}

       

        可以通过GestureListener.Instance.IsSwipeLeft()或GestureListener.Instance.IsSwipeRight()判断是否完成了对应挥手动作,从而实现具体逻辑。

        可以在其他对应接口里实现自己需要的逻辑。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/901179.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号