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

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

2.结构图如下:

3.在 Button1 上增加一个 Collider

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

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 是世界坐标的

6.效果图如下:

 

 

第二种方法 在你的脚本上增加一个插件自带的 ExtendedFingerDetector.cs 脚本

 if (ShowGizmos && HandModel != null && HandModel.IsTracked) {
        PointingState[] state = { Thumb, Index, Middle, Ring, Pinky };
        Hand hand = HandModel.GetLeapHand();
        int extendedCount = 0;
        int notExtendedCount = 0;
                Finger thumbFinger = hand.Fingers[0];
                Finger indexFinger = hand.Fingers[1];
                if (thumbFinger.IsExtended&&!indexFinger.IsExtended)
                {
                    print("大拇指张开,食指不张开");
                }
            }

 

  • 8
    点赞
  • 52
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值