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

标签 c# events unity3d leap-motion

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

我想使用 C# 委托(delegate)和事件将有关手势的信息在它们发生时传递给另一个脚本。在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 接口(interface)的 Controller ?)?

最佳答案

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

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

诚然这会产生一些前期设置成本,但由于无论如何都需要转换大部分飞跃数据才能在 Unity 中使用,所以最好将其抽象化,恕我直言。

J.

关于c# - 使用 C# 和 Unity 3D 在 Leap Motion 中启用手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19151910/

相关文章:

c# - 在 Unity 中顺畅地逐 block 移动播放器

c# - 为什么我的 Azure WebJob 没有安排?

javascript - jQuery 事件按键 : Which key was pressed?

android - 来自原生 Android 服务的 Unity3d 脚本

javascript - 为什么 "return"点击事件没有执行默认的浏览器行为?

c# - 从 .NET 附加到 COM 服务器中的事件时出现 E_ACCESSDENIED 异常

javascript - 从浏览器访问 Unity Webgl 输出,反之亦然

c# - UWP C# : The name xxx cannot be found in namespace yyy

c# - RegularExpressionAttribute 行为中的正则表达式

c# - 在 C# 中手动填充 DataTable 的最快方法