Skip to main content
 首页 » 资源教程 » Unity3D教程

Kinect for Unity V2 代码示例(二)

2016年09月01日 12:32:439090
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using UnityEngine.UI;  
  4.   
  5. public class UseOverKinectManager : MonoBehaviour {  
  6.   
  7.     public RawImage kinectImg;  
  8.     public Canvas canvas;  
  9.     public Image rightHand;  
  10.   
  11.     public Image btn1;  
  12.   
  13.     // Use this for initialization  
  14.     void Start () {  
  15.       
  16.     }  
  17.       
  18.     // Update is called once per frame  
  19.     void Update () {  
  20.         bool isInit = KinectManager.Instance.IsInitialized();  //检测设备可用  
  21.         if (isInit) {  
  22.   
  23.            // print("width = " + KinectManager.Instance.GetDepthImageWidth() + " height = " + KinectManager.Instance.GetDepthImageHeight()); //宽高  
  24.   
  25.             //设备准备好了  可以读取了  
  26.             if (kinectImg.texture == null)  
  27.             {  
  28.                 Texture2D kinectPic = KinectManager.Instance.GetUsersClrTex();  //从设备获取彩色数据  
  29.                 // Texture2D kinectPic = KinectManager.Instance.GetUsersLblTex();  //获取深度数据量  
  30.                 kinectImg.texture = kinectPic;  //把彩色数据给控件显示  
  31.             }  
  32.   
  33.             if (KinectManager.Instance.IsUserDetected())  
  34.             {  
  35.                 //检测到玩家  
  36.                 long userId = KinectManager.Instance.GetPrimaryUserID();  //获取用户id  
  37.   
  38.                 int jointType = (int)KinectInterop.JointType.HandRight;  
  39.                 if (KinectManager.Instance.IsJointTracked(userId,jointType))  
  40.                 {  
  41.                     //关节点被追踪到  
  42.                     Vector3 rightHandPos = KinectManager.Instance.GetJointKinectPosition(userId, jointType);  //1.获取关节点3D坐标  
  43.                     Vector3 rightHandScreenPos = Camera.main.WorldToScreenPoint(rightHandPos);  //2.关节点坐标转换成屏幕坐标  
  44.                     Vector2 rightHandSenPos = new Vector2(rightHandScreenPos.x, rightHandScreenPos.y);  
  45.                    // print("x = " + rightHandScreenPos.x + " y = " + rightHandScreenPos.y);  
  46.                     Vector2 rightHandUguiPos;  
  47.                     if(RectTransformUtility.ScreenPointToLocalPointInRectangle((RectTransform)canvas.transform, rightHandSenPos, null,out rightHandUguiPos))  
  48.                     {  
  49.                         //表示右手在矩形范围内  
  50.                         RectTransform rightRectTf =  rightHand.transform as RectTransform;  
  51.                         rightRectTf.anchoredPosition = rightHandUguiPos;  
  52.                     }  
  53.   
  54.                     if (RectTransformUtility.RectangleContainsScreenPoint(btn1.rectTransform,rightHandSenPos,null))  
  55.                     {  
  56.                         //手在按钮1上悬停  
  57.                        // print("手在按钮1上悬停");  
  58.                         KinectInterop.HandState rightHandState = KinectManager.Instance.GetRightHandState(userId);  
  59.                         if (rightHandState == KinectInterop.HandState.Closed)  
  60.                         {  
  61.                             //print("握拳选择了");  
  62.                         }  
  63.                     }  
  64.                     else  
  65.                     {  
  66.                        // print("离开");  
  67.                     }  
  68.                       
  69.   
  70.                 }  
  71.             }  
  72.               
  73.               
  74.         }  
  75.     }  
  76. }  
评论列表暂无评论
发表评论