第一步:创建一个工程,导入好kinect sdk ,做好前期准备工作;
第二步:找到Main Camera或者其他的只要是渲染UI的相机即可,将其Projection修改为Orthography。如图位置
第三步:创建一个按钮,设置tag为“button”,并绑定Box Collieder 2D。
第四步:修改Canvas的Cavas组件中的Render Mode为”Screen Space-Camera“,并为其tian添加Render Camera。如图所示:
第五步:直接上代码了,注释不多,只有射线部分有点,勉强看吧,相信kinect追踪大家能看懂的,都是直接套用。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class manager : MonoBehaviour {
[Tooltip("Index of the player, tracked by this component. 0 means the 1st player, 1 - the 2nd one, 2 - the 3rd one, etc.")]
public int player1Index = 0;
public int player2Index = 1;
[Tooltip("Smooth factor for cursor movement.")]
public float smoothFactor = 10f;
bool CastRay = false;
[Tooltip("Camera that will be used to overlay the 3D-objects over the background.")]
public Camera foregroundCamera;
public GameObject[] firstPeopleHands;
// Use this for initialization
void Start() {
}
// Update is called once per frame
void Update()
{
KinectManager manager = KinectManager.Instance;
if (manager && manager.IsInitialized())
{
int rightHandIndex = (int)KinectInterop.JointType.HandRight;
int leftHandIndex = (int)KinectInterop.JointType.HandLeft;
if (manager.IsUserDetected())
{
List<int> indexList = manager.GetTrackedBodyIndices();
if (indexList.Count == 1)//One people
{
long userId_1 = manager.GetUserIdByIndex(player1Index);
if (manager.IsJointTracked(userId_1, rightHandIndex))
{
Vector3 rightHandRawPos = manager.GetJointKinectPosition(userId_1, rightHandIndex);
Vector3 leftHandRawPos = manager.GetJointKinectPosition(userId_1, leftHandIndex);
if (rightHandRawPos != Vector3.zero)
{
CastRay = true;
if (CastRay)
{//获取追踪 并进行ui检测
Rect backgroundRect = foregroundCamera.pixelRect;
Vector3 posJointRightHand = manager.GetJointPosColorOverlay(userId_1, rightHandIndex, foregroundCamera, backgroundRect);
Vector3 posJointLeftHand = manager.GetJointPosColorOverlay(userId_1,
(int)KinectInterop.JointType.HandLeft, foregroundCamera, backgroundRect);
firstPeopleHands[0].transform.localPosition = posJointRightHand;
firstPeopleHands[1].transform.localPosition = posJointLeftHand;
CheckRay(posJointRightHand);
}
}
}
}
}
}
}
bool ison=false;
private void CheckRay(Vector3 startPos)
{//右手点击UI
Vector2 mousePos2D = new Vector2(startPos.x,startPos.y);
RaycastHit2D hits = Physics2D.Raycast(mousePos2D, Vector2.up);
if (hits.collider!=null&& hits.collider.gameObject.tag == "button")
{
if (ison==false)
{
ison = true;
print("点击了按钮!!!!");
}
}
}
}
另外闲扯一下(嘿嘿):
接触Kinect时间不长,希望能多多与大家交流,学习之路还很漫长,菜鸟在路上,向那些大佬膜拜,希望大家也能多分享,为了大家学习,少才一些坑,推荐一个qq交流群吧,身边一个大佬的:806091680,进来互相交流经验,共同进步