BodySourceManager.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using UnityEngine;
  2. using System.Collections;
  3. using Windows.Kinect;
  4. public class BodySourceManager : MonoBehaviour
  5. {
  6. private KinectSensor _Sensor;
  7. private BodyFrameReader _Reader;
  8. private Body[] _Data = null;
  9. public Body[] GetData()
  10. {
  11. return _Data;
  12. }
  13. void Start ()
  14. {
  15. _Sensor = KinectSensor.GetDefault();
  16. if (_Sensor != null)
  17. {
  18. _Reader = _Sensor.BodyFrameSource.OpenReader();
  19. if (!_Sensor.IsOpen)
  20. {
  21. _Sensor.Open();
  22. }
  23. }
  24. }
  25. void Update ()
  26. {
  27. if (_Reader != null)
  28. {
  29. var frame = _Reader.AcquireLatestFrame();
  30. if (frame != null)
  31. {
  32. if (_Data == null)
  33. {
  34. _Data = new Body[_Sensor.BodyFrameSource.BodyCount];
  35. }
  36. frame.GetAndRefreshBodyData(_Data);
  37. frame.Dispose();
  38. frame = null;
  39. }
  40. }
  41. }
  42. void OnApplicationQuit()
  43. {
  44. if (_Reader != null)
  45. {
  46. _Reader.Dispose();
  47. _Reader = null;
  48. }
  49. if (_Sensor != null)
  50. {
  51. if (_Sensor.IsOpen)
  52. {
  53. _Sensor.Close();
  54. }
  55. _Sensor = null;
  56. }
  57. }
  58. }