1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using SC.XR.Unity;
- public class XRRGBCamera : SingletonMono<XRRGBCamera>
- {
- 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 (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();
- }
- }
- }
|