DepthSourceManager.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using UnityEngine;
  2. using System.Collections;
  3. using Windows.Kinect;
  4. public class DepthSourceManager : MonoBehaviour
  5. {
  6. private KinectSensor _Sensor;
  7. private DepthFrameReader _Reader;
  8. private ushort[] _Data;
  9. public ushort[] GetData()
  10. {
  11. return _Data;
  12. }
  13. void Start ()
  14. {
  15. _Sensor = KinectSensor.GetDefault();
  16. if (_Sensor != null) {
  17. _Reader = _Sensor.DepthFrameSource.OpenReader ();
  18. _Data = new ushort[_Sensor.DepthFrameSource.FrameDescription.LengthInPixels];
  19. }
  20. }
  21. void Update ()
  22. {
  23. if (_Reader != null)
  24. {
  25. var frame = _Reader.AcquireLatestFrame();
  26. if (frame != null)
  27. {
  28. frame.CopyFrameDataToArray(_Data);
  29. frame.Dispose();
  30. frame = null;
  31. }
  32. }
  33. }
  34. void OnApplicationQuit()
  35. {
  36. if (_Reader != null)
  37. {
  38. _Reader.Dispose();
  39. _Reader = null;
  40. }
  41. if (_Sensor != null)
  42. {
  43. if (_Sensor.IsOpen)
  44. {
  45. _Sensor.Close();
  46. }
  47. _Sensor = null;
  48. }
  49. }
  50. }