Пример #1
0
    void Update()
    {
        if (BodySourceManager == null)
        {
            return;
        }

        _bodyManager = BodySourceManager.GetComponent <BodySourceManager>();
        if (_bodyManager == null)
        {
            return;
        }

        Kinect.Body[] data = _bodyManager.GetData();
        if (data == null)
        {
            return;
        }

        List <ulong> trackedIds = new List <ulong>();

        foreach (var body in data)
        {
            if (body == null)
            {
                continue;
            }

            if (body.IsTracked)
            {
                trackedIds.Add(body.TrackingId);
                break;
            }
        }

        List <ulong> knownIds = new List <ulong>(Bodies.Keys);

        // First delete untracked bodies
        foreach (ulong trackingId in knownIds)
        {
            if (!trackedIds.Contains(trackingId))
            {
                Destroy(Bodies[trackingId]);
                Bodies.Remove(trackingId);
            }
        }

        foreach (var body in data)
        {
            if (body == null)
            {
                continue;
            }

            if (body.IsTracked)
            {
                if (!Bodies.ContainsKey(body.TrackingId))
                {
                    GameObject   newBody   = new GameObject();
                    KinectPlayer newPlayer = Instantiate(Resources.Load("BlockPlayer") as GameObject, new Vector3(0, 0, 0), Quaternion.identity).GetComponent <KinectPlayer>();
                    newPlayer.Kinect = this;
                    if (players.Count == 0)
                    {
                        newPlayer.makeMainPlayer(true);
                    }
                    Bodies[body.TrackingId] = newPlayer.CreateBodyObject(body, newBody);
                    newPlayer.WarpToLocation(new Vector3(0, 1.5f, 0));
                    players.Add(newPlayer);

                    _bodyThread = new Thread(new ThreadStart(RefreshBody));
                    _bodyThread.Start();
                }

                foreach (KinectPlayer player in players)
                {
                    try
                    {
                        if (player.IsTracked())
                        {
                            player.gameObject.SetActive(true);
                            player.UpdateBodyObject();
                            player.AdjustBodyParts();
                        }
                        else
                        {
                            player.gameObject.SetActive(false);
                        }
                    }
                    catch (Exception e)
                    {
                        Debug.LogError("Error getting body");
                        Debug.LogError(e);
                    }
                }
                break;
            }
        }
    }