其实大部分跟 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("手张开");
}
}