using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using SC.XR.Unity; public class XRRGBCamera : SingletonMono { public bool isAuto = true; public static XRRGBCamera Instance; public Texture CaptureImage; public WebCamTexture RGBCamTexture; private void Awake() { Instance = this; } private void Start() { // 获取可用的摄像头设备 if (isAuto) playCamera(1280, 720); } public void playCamera(int w, int h) { if (this.GetComponent() != null) { this.GetComponent().color = Color.black; } if (this.GetComponent() != null) { this.GetComponent().color = Color.black; } return; if (RGBCamTexture != null && RGBCamTexture.isPlaying) { RGBCamTexture.Stop(); } // 将WebCamTexture绑定到RawImage组件以显示摄像头捕捉到的图像 WebCamDevice[] devices = WebCamTexture.devices; if (devices.Length == 0) { Debug.Log("No webcam devices found."); return; } // 使用第一个可用的摄像头设备(你可以根据需要选择其他设备) RGBCamTexture = new WebCamTexture(devices[0].name, w, h, 30); Debug.Log("开启摄像头" + devices[0].name); // 开始摄像头捕捉 RGBCamTexture.Play(); CaptureImage = RGBCamTexture; if (this.GetComponent() != null) { this.GetComponent().texture = CaptureImage; this.GetComponent().transform.localEulerAngles = new Vector3(0, 0, 0); } if (this.GetComponent() != null) { this.GetComponent().material.mainTexture = CaptureImage; this.GetComponent().transform.localEulerAngles = new Vector3(0, 0, 0); } Debug.Log("开启摄像头"); } public void Update() { } public void stopCamera() { if (RGBCamTexture != null && RGBCamTexture.isPlaying) { RGBCamTexture.Stop(); } } }