近几日开始自己做个Unity3D小游戏,由于手头有个LeapMotion,我就准备做个手打怪兽的游戏吧。发现,必须看leapmotion英文文档才行啊,原来leapmotion的插件包在unity store长久没有更新,反而在他们的网站才能下载到。如果你要做Unity3d+LeapMotion的应用,下面的内容必读!
Setting up a project in Unity 3D
The Leap Motion asset package includes in plugin files for using the Leap Motion device on Windows and Mac computers. This package includes Hand prefabs, scripts, and demo scenes to help you develop Leap Motion apps quickly.
Note: As of Unity 5.0, both the Pro and Personal versions of Unity support plugins. You no longer need to use a different asset package or different setup for the “free” version of Unity.
Get the Leap Motion asset package from: https://developer.leapmotion.com/downloads/unity.
Create a project
First, create a new Unity project in the usual way, i.e.:
- Open the Unity editor.
- Select File > New Project…
- Choose a name and save location.
- Click Create Project.
Then, import the Leap Motion asset package into the project:
-
Select the Unity Assets > Import Package > Custom Package menu command.
-
Locate the downloaded asset package and click Open.
The assets are imported into your project.
If you are importing the assets into a project that already contains Leap Motion assets, it is recommended that you delete the old assets first (make a backup). The Unity import process only adds new files and overwrites changed files. It does not remove obsolete files.
Adding Hands to a Scene
Place the HandController prefab below the point at which you want the hands to appear. The hands in the scene are positioned at the same position as your real hands relative to the real Leap Motion device. You can change the scale of HandController object to change the size of the hands and change the Hand Movement Scale setting to allow the hands to move a within a larger volume.
Change the hand graphics by dragging a different hand prefab to the Left/Right Graphics Model slot.
To add hands to a scene:
-
In the Project panel, locate the HandController prefab in
Assets/LeapMotionPrefabs
-
Drag the HandController prefab into the Scene view.
-
Set the transform position to the desired location.
To see hands, the HandController must be within the field of view of the camera.
-
Set transform scale to size the hands appropriately to the scene. A scale of 1 means the hands are rendered at their real size (which is often too small).
-
Set the Left and Right Hand Graphics Model properties of the HandController to the desired prefab from the HandModelsHuman or HandModelsNonHuman prefab folders. (There is generally no need to change the physics model from RigidHand.)
-
Play the scene.
You should see hands appear in the Hierarchy panel as well as in the Game view when you put your hands over the Leap Motion device. If you don’t see hands in the Hierarchy view, open the Visualizer from the Leap Motion control panel to make sure your Leap Motion device is connected and sending out tracking data. If the Unity hands are just out of view, you can pause the game and use the Scene view to locate them.
Accessing the Leap Motion API directly
In addition to the prefabs and scripts included in the asset package, you can write your own scripts to access tracking data from the Leap Motion 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
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.
The script, LeapUnityExtensions.cs
in Assets/LeapMotion/Scripts/Utils, provides helpful functions for converting vectors and matrices from the Leap Motion API classes to the Unity API classes – as well as converting the scale and coordinate systems.