Unity AzureKinect 初识(三) 与UGUI交互

其实大部分跟 kinect V2 的逻辑是一样的,不过一段时间之后会发现可能这样写更简洁,毕竟我们都是菜鸟,总是在不断学习中成长,可以看到这次的代码比我二代写的就更简洁了,基本上几行代码就能做到与UGUI的交互 

 

思路:

1.获取手握拳

2.看看手握拳时坐标是否在按钮上

 

一个场景要运行Azure 基本配置我就不说了,去看第二篇

 

第一种方式 RectTransformUtility.RectangleContainsScreenPoint 适合需要点击的按钮少的项目 

运行条件:

1. 需要有一个Camera.main 能获取到的主摄像头,或者你自己定义一个摄像头,把我代码里面的Camera.main替换一下

第一种方式:添加如下两脚本,Button拖拽给TintInteractionManagerDemo.cs脚本别忘了

效果如图,在按钮上握拳

代码:

using com.rfilkov.components;
using UnityEngine;
using UnityEngine.UI;

public class TintInteractionManagerDemo : MonoBehaviour, InteractionListenerInterface
{
    public Button testBtn;

    public bool HandClickDetected(ulong userId, int userIndex, bool isRightHand, Vector3 handScreenPos)
    {
        Debug.Log("手点击");
        return true;
    }

    // 握拳识别接口
    public void HandGripDetected(ulong userId, int userIndex, bool isRightHand, bool isHandInteracting, Vector3 handScreenPos)
    {
        //handScreenPos 坐标是0-1 范围的 需要转换成屏幕分辨率实际的坐标
        Vector2 uguiPos = new Vector2(handScreenPos.x * Screen.width, handScreenPos.y * Screen.height);
        // 判断手在按钮范围内
        // 这个地方需要注意一个问题,那就是 RectTransformUtility.RectangleContainsScreenPoint第三个参数需要填你的摄像头
        // 如果不填的话,它是只能识别到canvas overlay模式的  camera 模式是会一直返回false的
        bool b = RectTransformUtility.RectangleContainsScreenPoint(testBtn.GetComponent<RectTransform>(), uguiPos,Camera.main);
        if (b)
        {
            Debug.Log("在按钮上握拳!!!");
        }
    }

    public void HandReleaseDetected(ulong userId, int userIndex, bool isRightHand, bool isHandInteracting, Vector3 handScreenPos)
    {
        Debug.Log("手张开");
    }

}

 

第二种方式 通过射线检测方式 适合需要点击的按钮多的项目

using com.rfilkov.components;
using UnityEngine;
using UnityEngine.UI;

public class TintInteractionManagerDemo : MonoBehaviour, InteractionListenerInterface
{
    public Button button1;
    public Button button2;
    private void Start()
    {
        button1.onClick.AddListener(() =>
        {
            print("button1握拳通过射线检测方式点击了按钮");
        });
        button2.onClick.AddListener(() =>
        {
            print("button2握拳通过射线检测方式点击了按钮");
        });
    }


    private void Update()
    {
        // Test方法
        if (Input.GetMouseButtonDown(0))
        {

            RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
            if (hit.collider != null)
            {
                Debug.Log(hit.collider.gameObject.name);
                // 调用按钮对象
                hit.collider.GetComponent<Button>().onClick.Invoke();
            }
        }
    }
    public bool HandClickDetected(ulong userId, int userIndex, bool isRightHand, Vector3 handScreenPos)
    {
        //Debug.Log("手点击");
        return true;
    }

    // 握拳识别接口
    public void HandGripDetected(ulong userId, int userIndex, bool isRightHand, bool isHandInteracting, Vector3 handScreenPos)
    {
        // 第一种适合需要点击的按钮少的项目
        //handScreenPos 坐标是0-1 范围的 需要转换成屏幕分辨率实际的坐标
        Vector2 uguiPos = new Vector2(handScreenPos.x * Screen.width, handScreenPos.y * Screen.height);
         判断手在按钮范围内
         这个地方需要注意一个问题,那就是 RectTransformUtility.RectangleContainsScreenPoint第三个参数需要填你的摄像头
         如果不填的话,它是只能识别到canvas overlay模式的  camera 模式是会一直返回false的
        //bool b = RectTransformUtility.RectangleContainsScreenPoint(testBtn.GetComponent<RectTransform>(), uguiPos,Camera.main);
        //if (b)
        //{
        //    Debug.Log("在按钮上握拳!!!");
        //}

        //第二种方式 适合需要点击的按钮多的项目
        RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(uguiPos), Vector2.zero);
        if (hit.collider!=null)
        {
            Debug.Log(hit.collider.gameObject.name);
            // 调用按钮对象
            hit.collider.GetComponent<Button>().onClick.Invoke();
        }

    }

    public void HandReleaseDetected(ulong userId, int userIndex, bool isRightHand, bool isHandInteracting, Vector3 handScreenPos)
    {
        //  Debug.Log("手张开");
    }

}

 

 

 

 

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值