Question

I have been testing some things with the Leap Motion and Unity, using the provided UnitySandbox project and I am confused about some aspects of the LeapInput class.

I want to use C# delegates and events to pass information on gestures to another script whenever they occur. In the LeapInput class, comments say that event handling can be placed directly in the class. However, when I try to enable events on the static m_controller class, they don't seem to register.

Yet, when I have a separate script that inherits from MonoBehaviour that declares an instance of the Leap Controller class like so:

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);
}

Then, when I check for events in Update they seem to register fine:

// 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;
                }
            }
        }
    }

My question is two part: Why, when I try to enable events for the static m_controller in either a static start or static awake, does it not succeed? And why, when I enable events on an just an instance of the Leap.Controller event class does that succed(i.e. how does that change register with the controller that is interfacing with the Leap?)?

Était-ce utile?

La solution

Earlier versions of the Leap API had delegates and I remember reading warnings that they may not have been thread safe. That might be why they changed the API to a loop based update using the Frame class vs an event system.

On our leap projects we use a proxy that handles the leap Frame updates and extract either the default gestures or analyze the frame data for our custom ones. That class has delegates and/or sends events using whatever system in place (usually PureMVC).

Granted that's a bit of upfront cost to setup, but since most of the leap data needs to be converted to be used in Unity anyway, it's better to abstract it, IMHO.

ath

J.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top