123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- using UnityEngine;
- using System.Collections;
- using Windows.Kinect;
- public class ColorSourceManager : MonoBehaviour
- {
- public int ColorWidth { get; private set; }
- public int ColorHeight { get; private set; }
- private KinectSensor _Sensor;
- private ColorFrameReader _Reader;
- private Texture2D _Texture;
- private byte[] _Data;
-
- public Texture2D GetColorTexture()
- {
- return _Texture;
- }
-
- void Start()
- {
- _Sensor = KinectSensor.GetDefault();
-
- if (_Sensor != null)
- {
- _Reader = _Sensor.ColorFrameSource.OpenReader();
-
- var frameDesc = _Sensor.ColorFrameSource.CreateFrameDescription(ColorImageFormat.Rgba);
- ColorWidth = frameDesc.Width;
- ColorHeight = frameDesc.Height;
-
- _Texture = new Texture2D(frameDesc.Width, frameDesc.Height, TextureFormat.RGBA32, false);
- _Data = new byte[frameDesc.BytesPerPixel * frameDesc.LengthInPixels];
-
- if (!_Sensor.IsOpen)
- {
- _Sensor.Open();
- }
- }
- }
-
- void Update ()
- {
- if (_Reader != null)
- {
- var frame = _Reader.AcquireLatestFrame();
-
- if (frame != null)
- {
- frame.CopyConvertedFrameDataToArray(_Data, ColorImageFormat.Rgba);
- _Texture.LoadRawTextureData(_Data);
- _Texture.Apply();
-
- frame.Dispose();
- frame = null;
- }
- }
- }
- void OnApplicationQuit()
- {
- if (_Reader != null)
- {
- _Reader.Dispose();
- _Reader = null;
- }
-
- if (_Sensor != null)
- {
- if (_Sensor.IsOpen)
- {
- _Sensor.Close();
- }
-
- _Sensor = null;
- }
- }
- }
|