1. 程式人生 > >Unity + LeapMotion 識別食指伸出以及食指與 UGUI的 互動

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.效果圖如下: