Unity + LeapMotion 识别食指伸出以及食指与 UGUI的 交互

1.使用 leapmotion 自带的案例下继续开发

Unity + LeapMotion 识别食指伸出以及食指与 UGUI的 交互

2.结构图如下:

Unity + LeapMotion 识别食指伸出以及食指与 UGUI的 交互

3.在 Button1 上增加一个 Collider

Unity + LeapMotion 识别食指伸出以及食指与 UGUI的 交互

4.新建一个脚本,为了能识别左手和右手的食指,判断先后关系,进行同样的逻辑处理,所以我的代码会比较复杂点(如果只是识别一个手的食指就简单多了),将 BonesL和BonesR 的数组赋值,请一一对应,左手的赋值,右手的你自己来吧

Unity + LeapMotion 识别食指伸出以及食指与 UGUI的 交互

5.代码如下:

using System.Collections;
using System.Collections.Generic;
using Leap;
using Leap.Unity;
using UnityEngine;
using UnityEngine.UI;
public class JudgeHandDetector : MonoBehaviour {

    public Transform[] bonesL;
    public Transform[] bonesR;
    public HandModelBase leftHand;
    public HandModelBase rightHand;

    List<HandModelBase> handModelList = new  List<HandModelBase>();
    // Update is called once per frame
    void Update () {

        // 如果两个手都没有识别 清空 list 表
        if (!leftHand.IsTracked)
        {
            if (handModelList.Contains(leftHand))
            {
                handModelList.Remove(leftHand);
            }
        }
        if (!rightHand.IsTracked)
        {
            if (handModelList.Contains(rightHand))
            {
                handModelList.Remove(rightHand);
            }
        }
        // 鼠标移动 根据 list[0] 去操作,判断识别手的先后关系
        if (leftHand!= null && leftHand.IsTracked)
        {
            if (!handModelList.Contains(leftHand))
            {
                handModelList.Add(leftHand);
            }
        }
        if (rightHand != null && rightHand.IsTracked)
        {
            if (!handModelList.Contains(rightHand))
            {
                 handModelList.Add(rightHand);
            }
        }
        if (handModelList.Count > 0)
        {
            IndexDetector();
        }
	}
    Ray ray;
    /// <summary>
    /// 左右手食指识别 进入相同的处理逻辑
    /// </summary>
    void IndexDetector()
    {
        if (handModelList[0] == leftHand)
        {
            JudgeIndexDetector(bonesL);
        }
        else
        {
            JudgeIndexDetector(bonesR);
        }
    }

    /// <summary>
    /// 判断食指识别
    /// </summary>
    /// <param name="bones"></param>
    void JudgeIndexDetector(Transform[] bones)
    {
        if (bones[1].position.z > bones[0].position.z && bones[1].position.z > bones[2].position.z &&
              bones[1].position.z > bones[3].position.z && bones[1].position.z > bones[4].position.z)
        {
            print("这个傻逼伸出了邪恶的食指");
            DealRay(bones[1].position);
        }
    }
    /// <summary>
    /// 处理射线得到的信息
    /// </summary>
    void DealRay(Vector3 RayPointV3)
    {
        Vector2 screenPos = Camera.main.WorldToScreenPoint(RayPointV3);
        ray = Camera.main.ScreenPointToRay(screenPos);
        RaycastHit[] hit = Physics.RaycastAll(ray, 2000f, 1 << LayerMask.NameToLayer("UI")); ;
        if (hit.Length > 0)
        {
            for (int i = 0; i < hit.Length; i++)
            {
                Debug.Log("检测到物体" + hit[i].collider.name);
                BtnEvent(hit[i].transform);
            }
        }
    }
   

    void BtnEvent(Transform btn)
    {
        switch (btn.name)
        {
            case "Button1":
                btn.GetComponentInChildren<Text>().text = "把你的脏手从 Button1 上拿开";
                break;
            case "Button2":
                break;
            case "Button3":
                break;
            case "Button4":
                break;
            case "Button5":
                break;
            case "Button6":
                break;
            default:
                break;
        }
    }
}

5.对了 我的 canvas 是世界坐标的

Unity + LeapMotion 识别食指伸出以及食指与 UGUI的 交互

6.效果图如下:

Unity + LeapMotion 识别食指伸出以及食指与 UGUI的 交互

Unity + LeapMotion 识别食指伸出以及食指与 UGUI的 交互