2

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

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

然而,当我有一个从 MonoBehaviour 继承的单独脚本时,它声明了 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 接口的控制器?)?

4

1 回答 1

1

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

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

诚然,设置的前期成本有点高,但由于大多数跳跃数据无论如何都需要转换为在 Unity 中使用,所以最好将其抽象出来,恕我直言。

ath

J。

于 2013-10-03T20:22:28.387 回答