123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using SC.XR.Unity;
- public class XRRGBCamera : SingletonMono<XRRGBCamera>
- {
- public static XRRGBCamera Instance;
- public Texture CaptureImage;
- public WebCamTexture RGBCamTexture;
- public bool isAuto = true;
- private void Awake()
- {
- Instance = this;
- }
- private void Start()
- {
- // 获取可用的摄像头设备
- if(isAuto)
- playCamera(1280,720);
- }
- public void playCamera(int w, int h)
- {
- 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<RawImage>() != null)
- {
- this.GetComponent<RawImage>().texture = CaptureImage;
- this.GetComponent<RawImage>().transform.localEulerAngles = new Vector3(0, 0, 0);
- }
- if (this.GetComponent<MeshRenderer>() != null)
- {
- this.GetComponent<MeshRenderer>().material.mainTexture = CaptureImage;
- this.GetComponent<MeshRenderer>().transform.localEulerAngles = new Vector3(0, 0, 0);
- }
- Debug.Log("开启摄像头");
- }
- public void Update()
- {
- }
- public void stopCamera()
- {
- if (RGBCamTexture != null && RGBCamTexture.isPlaying)
- {
- RGBCamTexture.Stop();
- }
- }
- }
|