You can use the Leap Motion SDK as a Unity plug-in to access Leap Motion tracking data in a Unity application. This article describes how to use the plug-in mechanism supported by the Pro version of Unity. To use the standard version of Unity, which does not support native library plug-ins, please see, Creating Leap Motion apps with Unity (standard license).
Topics:
The Leap Motion SDK includes several versions of the native libraries to match the platform and architecture of your application's build targets.
To use the Leap Motion SDK in the Unity editor, you must add the 32-bit libraries to your application's Plugins folder. On the Mac, both 32-bit and 64-bit libraries are included in one file. However, on Windows, only one architecture can be included in a library file, so you must be sure to use the correct files.
Use the following libraries in Unity:
32-bit Windows:
64-bit Windows:
Note: Do not use the 64-bit libraries in the Unity editor on Windows.
32- and 64-bit Mac OS:
First, create a new Unity project in the usual way, i.e.:
Then, add the Leap Motion libraries to the project:
Note, you can place the libraries from both the Mac and the Windows SDK into your Plugins folder to enable cross-platform builds. However, see Building 64-bit Windows Unity applications for considerations when building for the x86 versus the x64 Windows platforms.
With the Leap Motion libraries in the Plugins folder, you can add script assets that use the API. The Leap Motion classes are defined in the Leap namespace. A basic MonoBehavior class that accesses the Leap Motion API will look something like the following:
using UnityEngine; using System.Collections; using Leap; public class LeapBehavior : MonoBehaviour { Controller controller; void Start () { controller = new Controller(); } void Update () { Frame frame = controller.Frame(); // do something with the tracking data in the frame... } }
Note that this example does not use a Leap.Listener subclass. Since a Unity application has a natural frame rate, the Update() function can get the current frame of data from the Leap Motion controller when the function is called by the Unity engine.
Note: Using the Listener mechanism can cause the Unity editor and Unity applications to freeze on exit on Windows. This is due to a threading issue that can occur when using callbacks between managed and unmanaged code in Unity. See `Exiting cleanly on Windows with Unity3D 3.5`_ for a workaround.
When you build a Leap-enabled application with Unity, the Unity editor copies the Leap Motion libraries from your Plugins folders automatically. You can even put a set of both Windows x86 and Mac OS Leap Motion libraries in the Plugins folder to enable cross-platform builds from either platform. However, the Leap Motion x86 libraries cannot coexist with the Leap Motion x64 libraries in the Plugins folder.
On Windows, the Unity editor requires that the Plugins folder contains the 32-bit (x86 platform) libraries in order to run your application during development. If you make a build for the x64 platform, Unity copies these 32-bit libraries to the application's output data folder. Before running your 64-bit application you must replace the 32-bit Leap Motion libraries with the 64-bit versions from the lib/x64 folder of the Leap Motion SDK.
On Mac, you can place either the 32-bit Windows Leap Motion libraries or the 64-bit Windows libraries in your Plugins folder, but not both. If you want to build for both Windows platforms on the Mac, you must make sure to use the correct version of the Leap Motion libraries. You can do this by copying the correct libraries into the application's output data folder after building your application.
Because of a problem with the way that Mono closes threads during application shutdown, the Unity3D editor and Leap-enabled applications can hang on exit. This issue affects Windows computers only and does not occur in Unity 4, which uses a different version of Mono. This issue also only occurs if you use the Leap Motion Listener method of handling Leap Motion events.
To avoid hanging on exit, add the following code to your application:
#if UNITY_STANDALONE_WIN [DllImport("mono", SetLastError=true)] static extern void mono_thread_exit(); #endif void OnApplicationQuit() { controller.Dispose(); listener.Dispose(); #if UNITY_STANDALONE_WIN && !UNITY_EDITOR && UNITY_3_5 mono_thread_exit (); #endif
When using this code snippet, controller must be a reference to your Leap.Controller object and ''listener`` must be a reference to your Leap.Listener subclass.
Copyright © 2012-2013 Leap Motion, Inc. All rights reserved.
Leap Motion proprietary and confidential. Not for distribution. Use subject to the terms of the Leap Motion SDK Agreement available at https://developer.leapmotion.com/sdk_agreement, or another agreement between Leap Motion and you, your company or other organization.