using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Leap;
using Leap.Unity;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class LeapTC : MonoBehaviour
{
// Start is called before the first frame update
private LeapProvider mProvider;
private Frame mFrame;
private Hand mHand;
private Vector leftPosition;
private Vector rightPosition;
private Hand IsLeftHand;
private Hand IsRightHand;
private bool IsLeftHandOpen = false;
private bool IsRightHandOpen = false;
private Vector3 rightFingerPosition;
public GameObject TimeTrip;
public UnityEngine.UI.Image TimeImg;
public float TimeMax = 5f;
private float CurrTime = 0;
private float Timebili=0;
void Start()
{
mProvider = FindObjectOfType<LeapProvider>() as LeapProvider;
// Debug.LogError( );
}
// Update is called once per frame
void Update()
{
mFrame = mProvider.CurrentFrame;//获取当前帧
TimeTrip.gameObject.SetActive(false);
if (mFrame.Hands.Count > 0)
{
foreach (var itemHands in mFrame.Hands)
{
if (itemHands.PalmVelocity.x > 1|| itemHands.PalmVelocity.x <-1)
{
}
else {
if (itemHands.IsLeft)
{
leftPosition = itemHands.StabilizedPalmPosition;
//print ("leftPosition" + leftPosition);
IsLeftHand = itemHands;
// IsLeftHandOpen= isOpenFullHand(IsLeftHand);
}
if (itemHands.IsRight)
{
rightPosition = itemHands.StabilizedPalmPosition;
//print ("rightPosition" + rightPosition);
IsRightHand = itemHands;
// IsRightHandOpen = isOpenFullHand(IsRightHand);
foreach (Finger finger in itemHands.Fingers)
{
Finger.FingerType type = finger.Type;
if (type == Finger.FingerType.TYPE_INDEX)
{
rightFingerPosition.x = finger.TipPosition.x;
rightFingerPosition.y = finger.TipPosition.y;
rightFingerPosition.z = finger.TipPosition.z;
Vector2 screenPos = Camera.main.WorldToScreenPoint(rightFingerPosition);
// Debug.LogError(screenPos);
Ray ray = Camera.main.ScreenPointToRay(screenPos);
// RaycastHit[] hita = Physics.RaycastAll(ray, 200000f);
RaycastHit[] hita = Physics.RaycastAll(ray, 200000f, 1 << LayerMask.NameToLayer("UI")); ;
if (hita.Length > 0)
{
for (int i = 0; i < hita.Length; i++)
{
Debug.Log("检测到物体" + hita[i].collider.name);
Debug.DrawLine(ray.origin, hita[i].point);
// BtnEvent(hit[i].transform);
//自动执行按钮对应的回调函数
TimeTrip.gameObject.SetActive(true);
TimeTrip.GetComponent<RectTransform>().anchoredPosition = hita[i].collider.gameObject.GetComponent<RectTransform>().anchoredPosition;
CurrTime = CurrTime + Time.deltaTime ;
Timebili = CurrTime / TimeMax;
if (Timebili > 1)
{
Timebili = 1;
ExecuteEvents.Execute<IPointerClickHandler>(hita[i].collider.gameObject,
new PointerEventData(EventSystem.current),
ExecuteEvents.pointerClickHandler);
//按钮上加入box collider
//按钮加入 event trigger 并加入point click 事件
}
TimeImg.fillAmount = Timebili;
}
}
else {
CurrTime = 0;
}
}
//获取中指的信息finger_INDEX
}
}
}
// Debug.Log(IsLeftHandOpen + " " + IsRightHandOpen);
}
if (mFrame.Hands.Count>=2)
{
}
}
}
}