WebcamCaptureDemo.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using UnityEngine;
  2. using System.Collections;
  3. //-----------------------------------------------------------------------------
  4. // Copyright 2012-2017 RenderHeads Ltd. All rights reserved.
  5. //-----------------------------------------------------------------------------
  6. namespace RenderHeads.Media.AVProMovieCapture.Demos
  7. {
  8. /// <summary>
  9. /// Allows the user to select from a list of webcams and creates a capture instance per webcam allowing each webcam to be recorded
  10. /// </summary>
  11. public class WebcamCaptureDemo : MonoBehaviour
  12. {
  13. private class Instance
  14. {
  15. public string name;
  16. public WebCamTexture texture;
  17. public CaptureFromTexture capture;
  18. public CaptureGUI gui;
  19. }
  20. [SerializeField]
  21. private GUISkin _skin;
  22. [SerializeField]
  23. private GameObject _prefab;
  24. [SerializeField]
  25. private int _webcamResolutionWidth = 640;
  26. [SerializeField]
  27. private int _webcamResolutionHeight = 480;
  28. [SerializeField]
  29. private int _webcamFrameRate = 30;
  30. // State
  31. private Instance[] _instances;
  32. private int _selectedWebcamIndex;
  33. private void Start()
  34. {
  35. // Create instance data per webcam
  36. int numCams = WebCamTexture.devices.Length;
  37. _instances = new Instance[numCams];
  38. for (int i = 0; i < numCams; i++)
  39. {
  40. GameObject go = (GameObject)GameObject.Instantiate(_prefab);
  41. Instance instance = new Instance();
  42. instance.name = WebCamTexture.devices[i].name;
  43. instance.capture = go.GetComponent<CaptureFromTexture>();
  44. instance.capture._autoFilenamePrefix = "Demo4Webcam-" + i;
  45. instance.gui = go.GetComponent<CaptureGUI>();
  46. instance.gui._showUI = false;
  47. _instances[i] = instance;
  48. }
  49. if (numCams > 0)
  50. {
  51. Change(0);
  52. }
  53. }
  54. private void StartWebcam(Instance instance)
  55. {
  56. // NOTE: WebcamTexture can be slow for high resolutions, this can cause issues with audio-video sync.
  57. // Our plugins AVPro Live Camera or AVPro DeckLink can be used to capture high resolution devices
  58. instance.texture = new WebCamTexture(instance.name, _webcamResolutionWidth, _webcamResolutionHeight, _webcamFrameRate);
  59. instance.texture.Play();
  60. if (instance.texture.isPlaying)
  61. {
  62. instance.capture.SetSourceTexture(instance.texture);
  63. }
  64. else
  65. {
  66. StopWebcam(instance);
  67. }
  68. }
  69. private void StopWebcam(Instance instance)
  70. {
  71. if (instance.texture != null)
  72. {
  73. if (instance.capture != null && instance.capture.IsCapturing())
  74. {
  75. instance.capture.SetSourceTexture(null);
  76. instance.capture.StopCapture();
  77. }
  78. instance.texture.Stop();
  79. Destroy(instance.texture);
  80. instance.texture = null;
  81. }
  82. }
  83. private void OnDestroy()
  84. {
  85. for (int i = 0; i < _instances.Length; i++)
  86. {
  87. StopWebcam(_instances[i]);
  88. }
  89. }
  90. private void Change(int index)
  91. {
  92. _selectedWebcamIndex = index;
  93. for (int j = 0; j < _instances.Length; j++)
  94. {
  95. _instances[j].gui._showUI = (j == _selectedWebcamIndex);
  96. }
  97. }
  98. private void OnGUI()
  99. {
  100. GUI.skin = _skin;
  101. GUILayout.BeginArea(new Rect(Screen.width - 512, 0, 512, Screen.height));
  102. GUILayout.BeginVertical();
  103. GUILayout.Label("Select webcam:");
  104. for (int i = 0; i < _instances.Length; i++)
  105. {
  106. Instance webcam = _instances[i];
  107. GUILayout.BeginHorizontal(GUILayout.ExpandWidth(true));
  108. if (_selectedWebcamIndex == i)
  109. {
  110. GUILayout.Label("->", GUILayout.Width(32f));
  111. }
  112. else
  113. {
  114. GUILayout.Label(" ", GUILayout.Width(32f));
  115. }
  116. if (webcam.capture.IsCapturing())
  117. {
  118. float t = Mathf.PingPong(Time.timeSinceLevelLoad, 0.25f) * 4f;
  119. GUI.backgroundColor = Color.Lerp(GUI.backgroundColor, Color.white, t);
  120. GUI.color = Color.Lerp(Color.red, Color.white, t);
  121. }
  122. if (GUILayout.Button(webcam.name, GUILayout.Width(200), GUILayout.ExpandWidth(true)))
  123. {
  124. Change(i);
  125. }
  126. GUI.backgroundColor = Color.white;
  127. GUI.color = Color.white;
  128. if (webcam.texture == null)
  129. {
  130. if (GUILayout.Button("Play", GUILayout.Width(64f)))
  131. {
  132. StartWebcam(webcam);
  133. Change(i);
  134. }
  135. }
  136. else
  137. {
  138. if (GUILayout.Button("Stop", GUILayout.Width(64f)))
  139. {
  140. StopWebcam(webcam);
  141. Change(i);
  142. }
  143. }
  144. if (webcam.texture != null)
  145. {
  146. Rect camRect = GUILayoutUtility.GetRect(256, 256.0f / (webcam.texture.width / (float)webcam.texture.height));
  147. GUI.DrawTexture(camRect, webcam.texture);
  148. }
  149. else
  150. {
  151. GUILayout.Label("No signal...", GUILayout.MinWidth(256.0f), GUILayout.MaxWidth(256.0f), GUILayout.ExpandWidth(false));
  152. }
  153. GUILayout.EndHorizontal();
  154. }
  155. GUILayout.EndVertical();
  156. GUILayout.EndArea();
  157. }
  158. }
  159. }