Kinect for Unity V2 代码示例(二)

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("离开");
                    }
                    

                }
            }
            
            
        }
	}
}

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jswm20150115

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值