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. {
  18. _Reader = _Sensor.DepthFrameSource.OpenReader();
  19. _Data = new ushort[_Sensor.DepthFrameSource.FrameDescription.LengthInPixels];
  20. }
  21. }
  22. void Update ()
  23. {
  24. if (_Reader != null)
  25. {
  26. var frame = _Reader.AcquireLatestFrame();
  27. if (frame != null)
  28. {
  29. frame.CopyFrameDataToArray(_Data);
  30. frame.Dispose();
  31. frame = null;
  32. }
  33. }
  34. }
  35. void OnApplicationQuit()
  36. {
  37. if (_Reader != null)
  38. {
  39. _Reader.Dispose();
  40. _Reader = null;
  41. }
  42. if (_Sensor != null)
  43. {
  44. if (_Sensor.IsOpen)
  45. {
  46. _Sensor.Close();
  47. }
  48. _Sensor = null;
  49. }
  50. }
  51. }