XRRGBCamera.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using SC.XR.Unity;
  6. public class XRRGBCamera : SingletonMono<XRRGBCamera>
  7. {
  8. public static XRRGBCamera Instance;
  9. public Texture CaptureImage;
  10. public WebCamTexture RGBCamTexture;
  11. private void Awake()
  12. {
  13. Instance = this;
  14. }
  15. private void Start()
  16. {
  17. // 获取可用的摄像头设备
  18. playCamera();
  19. }
  20. public void playCamera()
  21. {
  22. // 将WebCamTexture绑定到RawImage组件以显示摄像头捕捉到的图像
  23. WebCamDevice[] devices = WebCamTexture.devices;
  24. if (devices.Length == 0)
  25. {
  26. Debug.Log("No webcam devices found.");
  27. return;
  28. }
  29. // 使用第一个可用的摄像头设备(你可以根据需要选择其他设备)
  30. RGBCamTexture = new WebCamTexture(devices[0].name, 1280, 720, 30);
  31. Debug.Log("开启摄像头" + devices[0].name);
  32. // 开始摄像头捕捉
  33. RGBCamTexture.Play();
  34. CaptureImage = RGBCamTexture;
  35. if (this.GetComponent<RawImage>()!=null)
  36. {
  37. this.GetComponent<RawImage>().texture = CaptureImage;
  38. this.GetComponent<RawImage>().transform.localEulerAngles = new Vector3(0,0,0);
  39. }
  40. Debug.Log("开启摄像头");
  41. }
  42. public void Update()
  43. {
  44. }
  45. public void stopCamera()
  46. {
  47. RGBCamTexture.Stop();
  48. }
  49. }