使用 C# 和 Unity 3D 在 Leap Motion 中启用手势

本文关键字:Motion 启用 Leap Unity 3D 使用 | 更新日期: 2023-09-27 18:33:36

我一直在使用提供的UnitySandbox项目测试Leap Motion和Unity的一些东西,我对LeapInput类的某些方面感到困惑。

我想使用 C# 委托和事件在手势发生时将有关手势的信息传递给另一个脚本。 在 LeapInput 类中,注释说事件处理可以直接放在类中。 但是,当我尝试在静态m_controller类上启用事件时,它们似乎没有注册。

然而,当我有一个继承自 MonoBehavior 的单独脚本时,该脚本声明了 Leap Controller 类的实例,如下所示:

using UnityEngine;
using System.Collections;
using Leap;
public class LeapToUnityInterface : MonoBehaviour {
Leap.Controller controller;
#region delegates
#endregion
#region events
#endregion
// Use this for initialization
void Start () 
{
    controller = new Controller();
    controller.EnableGesture(Gesture.GestureType.TYPECIRCLE);
    controller.EnableGesture(Gesture.GestureType.TYPEINVALID);
    controller.EnableGesture(Gesture.GestureType.TYPEKEYTAP);
    controller.EnableGesture(Gesture.GestureType.TYPESCREENTAP);
    controller.EnableGesture(Gesture.GestureType.TYPESWIPE);
}

然后,当我在更新中检查事件时,它们似乎注册良好:

// Update is called once per frame
    void Update () 
    {
        Frame frame = controller.Frame();
        foreach (Gesture gesture in frame.Gestures())
        {
            switch(gesture.Type)
            {
                case(Gesture.GestureType.TYPECIRCLE):
                {
                    Debug.Log("Circle gesture recognized.");
                    break;
                }
                case(Gesture.GestureType.TYPEINVALID):
                {
                    Debug.Log("Invalid gesture recognized.");
                    break;
                }
                case(Gesture.GestureType.TYPEKEYTAP):
                {
                    Debug.Log("Key Tap gesture recognized.");
                    break;
                }
                case(Gesture.GestureType.TYPESCREENTAP):
                {
                    Debug.Log("Screen tap gesture recognized.");
                    break;
                }
                case(Gesture.GestureType.TYPESWIPE):
                {
                    Debug.Log("Swipe gesture recognized.");
                    break;
                }
                default:
                {
                    break;
                }
            }
        }
    }

我的问题分为两部分:为什么当我尝试在静态启动或静态唤醒中为静态m_controller启用事件时,它没有成功?为什么,当我在 Leap.Controller 事件类的一个实例上启用事件时,它是否成功(即,该更改如何向与 Leap 接口的控制器注册?

使用 C# 和 Unity 3D 在 Leap Motion 中启用手势

早期版本的 Leap API 有委托,我记得读到警告说它们可能不是线程安全的。这可能就是他们使用 Frame 类与事件系统将 API 更改为基于循环的更新的原因。

在我们的 leap 项目中,我们使用一个代理来处理 leap Frame 更新并提取默认手势或分析自定义手势的帧数据。该类具有委托和/或使用任何适当的系统(通常是 PureMVC(发送事件。

当然,这是设置的前期成本,但由于大多数 leap 数据无论如何都需要转换才能在 Unity 中使用,因此最好将其抽象化,恕我直言。

阿特

J.