首先是硬件的安装,安装硬件的应用软件Leap_Motion_Setup_4.0.0+52173.exe,按照提示默认安装,安装好之后,插入leapmotion硬件,将绿色灯的一面朝向自己。运行软件LeapMotionControlPanel,可以在右下角看到一个绿色的图标,如下图,就说明软件可以正常使用了。
   如果你的图标不是绿色的,是黄色的说明硬件的摄像头存在污渍,你可以用干净的布对硬件的摄像头进行擦拭解决。如果显示是红色的灯问题就比较大了,说明硬件不能正常工作,你可以打开LeapMotionControlPanel的可视化页面进行查找问题,如下图,也记得检查一下USB的连接线,最好是插在电脑的USB3.0的接口。  硬件的问题准备就绪了,下面就可以开始程序的开发了。 首先是新建一个工程,我用的是2019.4版本的,创建好工程后就可以导入Leapmotion的SDK包了,SDK包可以去Leapmotion的官网根据自己的Unity版本去进行合适的下载。一般导入核心模块就可以进行开发了。SDK包里面的例子也是很丰富的,可以根据自己的需要进行学习。 这个时候我们运行插件的Demo,发现功能是正常的,那么接下来我们就可以继续开发了。 
首先我们新建一个空的场景。若想接受到Leapmotion的手势信号,核心组件就是LeapProvider,我这边新建了一个空物体@Leap来管理Leapmotin的核心组件,我在预制体里面找到了LeapHandController,将其挂载在@Leap下,这样我们就可以接受到硬件的手势信号了。  接下来就是写脚本了,我新建了一个脚本Control,将其挂载到了@Control上了,在脚本里面写我们的核心逻辑然后运行,功能都是正常的。 
using Leap;
using Leap.Unity;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace LJF
{
public class Control : MonoBehaviour
{
private LeapProvider lp;
private Frame fr;
private Hand hand;
private float ttm = 0;
private void Start()
{
lp = FindObjectOfType<LeapProvider>() as LeapProvider;
}
private void Update()
{
fr = lp.CurrentFrame;
if (fr.Hands.Count >= 1)
{
List<Hand> handList = fr.Hands;
hand = handList[0];
if (isMoveLeft(hand))
{
ttm += Time.deltaTime;
if (ttm >= 0.2f)
{
ttm = 0;
Debug.Log("左滑");
}
}
}
}
[Tooltip("Velocity (m/s) move toward ")]
protected float deltaVelocity = 0.7f;
protected bool isMoveLeft(Hand hand)
{
return hand.PalmVelocity.x < -deltaVelocity && !isStationary(hand);
}
protected bool isMoveRight(Hand hand)
{
return hand.PalmVelocity.x > deltaVelocity && !isStationary(hand);
}
protected bool isMoveUp(Hand hand)
{
return hand.PalmVelocity.y > deltaVelocity && !isStationary(hand);
}
protected bool isMoveDown(Hand hand)
{
return hand.PalmVelocity.y < -deltaVelocity && !isStationary(hand);
}
protected bool isStationary(Hand hand)
{
return hand.PalmVelocity.Magnitude < smallestVelocity;
}
[Tooltip("Velocity (m/s) move toward ")]
protected float smallestVelocity = 0.4f;
protected bool isGrabHand(Hand hand)
{
return hand.GrabStrength > 0.8f;
}
protected bool isOpenFullHand(Hand hand)
{
return hand.GrabStrength == 0;
}
}
}
如果我们不需要手的模型,只是用来接受手势信号进行逻辑开发,上面的就已经够用了,如果我们需要手的模型那么接下来继续看吧。
如果需要手的模型进行控制,那么我们可以创建一个空物体@Hand,给他挂载脚本HandModelManager,然后在leapmotion的预制体里面找到两个手的模型将其也放到@hand下面,然后将两个手添加到HandModelManager的属性里,记得勾选isEnable。如果需要调整手模型的位置,这个时候你直接调整手的预制体位置是没有用的,他的位置是受核心模块LeapProvider控制的,我们可以通过调整上面我们创建的@Leap的位置来调整手的位置。 
如果我们还需要获取一些手的手的判断事件,可以添加下图文件夹的这些脚本来进行实现。这些根据需要大家可以直接摸索。 
上面用的的插件和软件我这边放到网盘里分享给大家,需要的可以来这里自取。 链接:https://pan.baidu.com/s/17jM7HcP12r3zbbrbmEdBkg 提取码:manp –来自百度网盘超级会员V1的分享
|