CameraCaptureController.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. using UnityEngine;
  10. using UnityEngine.UI;
  11. namespace NRKernal.NRExamples
  12. {
  13. /// <summary> A controller for handling camera captures. </summary>
  14. [HelpURL("https://developer.nreal.ai/develop/unity/rgb-camera")]
  15. public class CameraCaptureController : MonoBehaviour
  16. {
  17. /// <summary> The capture image. </summary>
  18. public RawImage CaptureImage;
  19. /// <summary> Number of frames. </summary>
  20. // public Text FrameCount;
  21. /// <summary> Gets or sets the RGB camera texture. </summary>
  22. /// <value> The RGB camera texture. </value>
  23. private NRRGBCamTexture RGBCamTexture { get; set; }
  24. void Start()
  25. {
  26. RGBCamTexture = new NRRGBCamTexture();
  27. CaptureImage.texture = RGBCamTexture.GetTexture();
  28. RGBCamTexture.Play();
  29. }
  30. /// <summary> Updates this object. </summary>
  31. void Update()
  32. {
  33. if (RGBCamTexture == null)
  34. {
  35. return;
  36. }
  37. // FrameCount.text = RGBCamTexture.FrameCount.ToString();
  38. }
  39. /// <summary> Plays this object. </summary>
  40. public void Play()
  41. {
  42. if (RGBCamTexture == null)
  43. {
  44. RGBCamTexture = new NRRGBCamTexture();
  45. CaptureImage.texture = RGBCamTexture.GetTexture();
  46. }
  47. RGBCamTexture.Play();
  48. // The origin texture will be destroyed after call "Stop",
  49. // Rebind the texture.
  50. CaptureImage.texture = RGBCamTexture.GetTexture();
  51. }
  52. /// <summary> Pauses this object. </summary>
  53. public void Pause()
  54. {
  55. RGBCamTexture?.Pause();
  56. }
  57. /// <summary> Stops this object. </summary>
  58. public void Stop()
  59. {
  60. RGBCamTexture?.Stop();
  61. RGBCamTexture = null;
  62. }
  63. /// <summary> Executes the 'destroy' action. </summary>
  64. void OnDestroy()
  65. {
  66. RGBCamTexture?.Stop();
  67. RGBCamTexture = null;
  68. }
  69. }
  70. }