using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class UseOverKinectManager : MonoBehaviour {
public RawImage kinectImg;
public Canvas canvas;
public Image rightHand;
public Image btn1;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
bool isInit = KinectManager.Instance.IsInitialized(); //检测设备可用
if (isInit) {
// print("width = " + KinectManager.Instance.GetDepthImageWidth() + " height = " + KinectManager.Instance.GetDepthImageHeight()); //宽高
//设备准备好了 可以读取了
if (kinectImg.texture == null)
{
Texture2D kinectPic = KinectManager.Instance.GetUsersClrTex(); //从设备获取彩色数据
// Texture2D kinectPic = KinectManager.Instance.GetUsersLblTex(); //获取深度数据量
kinectImg.texture = kinectPic; //把彩色数据给控件显示
}
if (KinectManager.Instance.IsUserDetected())
{
//检测到玩家
long userId = KinectManager.Instance.GetPrimaryUserID(); //获取用户id
int jointType = (int)KinectInterop.JointType.HandRight;
if (KinectManager.Instance.IsJointTracked(userId,jointType))
{
//关节点被追踪到
Vector3 rightHandPos = KinectManager.Instance.GetJointKinectPosition(userId, jointType); //1.获取关节点3D坐标
Vector3 rightHandScreenPos = Camera.main.WorldToScreenPoint(rightHandPos); //2.关节点坐标转换成屏幕坐标
Vector2 rightHandSenPos = new Vector2(rightHandScreenPos.x, rightHandScreenPos.y);
// print("x = " + rightHandScreenPos.x + " y = " + rightHandScreenPos.y);
Vector2 rightHandUguiPos;
if(RectTransformUtility.ScreenPointToLocalPointInRectangle((RectTransform)canvas.transform, rightHandSenPos, null,out rightHandUguiPos))
{
//表示右手在矩形范围内
RectTransform rightRectTf = rightHand.transform as RectTransform;
rightRectTf.anchoredPosition = rightHandUguiPos;
}
if (RectTransformUtility.RectangleContainsScreenPoint(btn1.rectTransform,rightHandSenPos,null))
{
//手在按钮1上悬停
// print("手在按钮1上悬停");
KinectInterop.HandState rightHandState = KinectManager.Instance.GetRightHandState(userId);
if (rightHandState == KinectInterop.HandState.Closed)
{
//print("握拳选择了");
}
}
else
{
// print("离开");
}
}
}
}
}
}
Kinect for Unity V2 代码示例(二)
最新推荐文章于 2024-06-26 16:20:03 发布