Module_RGBCamera.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace SC.XR.Unity
  5. {
  6. public class Module_RGBCamera : MonoBehaviour
  7. {
  8. Vector3 offset = new Vector3(0.03f, -0.01f, 0);
  9. [HideInInspector]
  10. public WebCamTexture webTex;
  11. [HideInInspector]
  12. public string deviceName;
  13. public static Module_RGBCamera Instance;
  14. private static MeshRenderer mat;
  15. float _width;
  16. float _height;
  17. void Start()
  18. {
  19. if (Instance == null)
  20. {
  21. Screen.sleepTimeout = SleepTimeout.NeverSleep;
  22. Instance = this;
  23. this.gameObject.transform.localPosition = offset;
  24. _width = Screen.width / 2.0f;
  25. _height = Screen.height;
  26. this.gameObject.transform.localScale = new Vector3(1, _height / _width, 1);
  27. mat = this.gameObject.GetComponent<MeshRenderer>();
  28. if (API_Module_Device.CurrentAndroid.HasRGB)
  29. {
  30. StartCoroutine(CallCamera());
  31. }
  32. else
  33. {
  34. Debug.Log("LGS:This device does not support the RGB");
  35. }
  36. }
  37. }
  38. public void StartCamera()
  39. {
  40. if (API_Module_Device.CurrentAndroid.HasRGB)
  41. {
  42. StartCoroutine(CallCamera());
  43. }
  44. }
  45. IEnumerator CallCamera()
  46. {
  47. yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
  48. if (Application.HasUserAuthorization(UserAuthorization.WebCam))
  49. {
  50. WebCamDevice[] devices = WebCamTexture.devices;
  51. deviceName = devices[0].name;
  52. if (webTex != null)
  53. {
  54. webTex.Stop();
  55. webTex = null;
  56. }
  57. //Set RGBCamera rect
  58. webTex = new WebCamTexture(deviceName, Screen.width / 2, Screen.height, 30);//
  59. mat.material.mainTexture = webTex;
  60. webTex.Play();//Start
  61. }
  62. }
  63. public void StopCamera()
  64. {
  65. if (Application.HasUserAuthorization(UserAuthorization.WebCam))
  66. {
  67. mat.material.mainTexture = null;
  68. webTex.Stop();
  69. webTex = null;
  70. StopCoroutine(CallCamera());
  71. }
  72. }
  73. }
  74. }