我尝试了一些脚本来控制层次结构中的对象,并且效果很好,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Windows.Kinect;
public class DetectJoint : MonoBehaviour {
public GameObject BodySrcManager;
public JointType TrackedJoint;
private BodySourceManager bodyManager;
private Body[] bodies = null;
public float multiplier = 10f;
void Start()
{
if (BodySrcManager == null)
{
Debug.Log("Assign Game Objects with body source manager");
}
else
{
bodyManager = BodySrcManager.GetComponent<BodySourceManager>();
}
}
void Update()
{
if (bodyManager == null)
{
Debug.Log("bodyManager is Null");
return;
}
bodies = bodyManager.GetData();
if (bodies == null)
{
Debug.Log("bodies is Null");
return;
}
foreach (var body in bodies)
{
if (body == null)
{
continue;
}
if (body.IsTracked)
{
var pos = body.Joints[TrackedJoint].Position;
gameObject.transform.position = new Vector3(pos.X * multiplier, pos.Y * multiplier);
}
}
}
}
和一个名为BodySourceManager的脚本
using UnityEngine;
using System.Collections;
using Windows.Kinect;
public class BodySourceManager : MonoBehaviour
{
private KinectSensor _Sensor;
private BodyFrameReader _Reader;
private Body[] _Data = null;
public Body[] GetData()
{
return _Data;
}
void Start ()
{
_Sensor = KinectSensor.GetDefault();
if (_Sensor != null)
{
_Reader = _Sensor.BodyFrameSource.OpenReader();
if (!_Sensor.IsOpen)
{
_Sensor.Open();
}
}
}
void Update ()
{
if (_Reader != null)
{
var frame = _Reader.AcquireLatestFrame();
if (frame != null)
{
if (_Data == null)
{
_Data = new Body[_Sensor.BodyFrameSource.BodyCount];
}
frame.GetAndRefreshBodyData(_Data);
frame.Dispose();
frame = null;
}
}
}
void OnApplicationQuit()
{
if (_Reader != null)
{
_Reader.Dispose();
_Reader = null;
}
if (_Sensor != null)
{
if (_Sensor.IsOpen)
{
_Sensor.Close();
}
_Sensor = null;
}
}
}
问题是我无法控制来自Prefabs的实例化
任何想法怎么做?