如何在Kinect For Unity 中显示玩家头像

以下就是脚本代码,直接使用就行了

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class GetPlayerIcon : MonoBehaviour
{
    private KinectManager _kinectManager;
    private int _headIndex = (int)KinectInterop.JointType.Head;

    public GameObject objPlayerIcon;
    private Texture2D destTex;

    // Use this for initialization
    void Start()
    {
        _kinectManager = KinectManager.Instance;
        destTex = new Texture2D(200, 200);
    }

    // Update is called once per frame
    void Update()
    {
        if (_kinectManager.GetUsersCount() >= 1)
        {
            if (GameControl._playerId != 0)
            {
                objPlayerIcon.GetComponent<RawImage>().texture = GetIcon(GameControl._playerId);
            }

            //objPlayerIcon.GetComponent<GUITexture>().texture = GetIcon(DragonDanceS._playerId);
        }
    }

    /// <summary>
    /// 获取玩家图像
    /// </summary>
    /// <param name="userId">玩家的kinect playerId</param>
    /// <returns></returns>
    private Texture2D GetIcon(long userId)
    {
        if (!JointTracked(userId, _headIndex))
        {
            return null;
        }
        else
        {
            Vector2 posDepth = _kinectManager.MapSpacePointToDepthCoords(JointPosition(userId, _headIndex));
            ushort depthValue = _kinectManager.GetDepthForPixel((int)posDepth.x, (int)posDepth.y);
            Vector2 posColor = _kinectManager.MapDepthPointToColorCoords(posDepth, depthValue);
            Texture2D sourceTex = _kinectManager.GetUsersClrTex();
            //Texture2D destTex = new Texture2D(200, 200);
            Color[] pix = sourceTex.GetPixels((int)Mathf.Clamp((int)posColor.x - 100, 0, (sourceTex.width - 200)), (int)Mathf.Clamp((int)(posColor.y - 100), 0, (sourceTex.height - 200)), 200, 200);
            for (int i = 0; i < destTex.height; i++)
            {
                for (int j = 0; j < destTex.width; j++)
                {
                    destTex.SetPixel(j, i, pix[(destTex.height - i - 1) * destTex.width + j]);
                }
            }
            destTex.Apply();

            return destTex;
        }
    }

    /// <summary>
    /// 获取骨骼点相对Kinect的坐标
    /// </summary>
    /// <param name="userId">玩家Id</param>
    /// <param name="joint">骨骼点</param>
    /// <returns></returns>
    private Vector3 JointPosition(long userId, int joint)
    {
        return _kinectManager.GetJointKinectPosition(userId, joint);
    }

    /// <summary>
    /// 判断骨骼点是否被检测到
    /// </summary>
    /// <param name="userId"></param>
    /// <param name="joint"></param>
    /// <returns></returns>
    private bool JointTracked(long userId, int joint)
    {
        return _kinectManager.IsJointTracked(userId, joint);
    }
}

注意:
1.新建一个RawImage(必须是RawImage),然后将这个脚本挂载到RawImage节点上
2.必须要勾选KinectManger中的Compute Color Map选项!!!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值