using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class OpenCamera : MonoBehaviour { // UI 相关参数 public RawImage rawImage; // 摄像机图片参数 private WebCamTexture webCamTexture; // Start is called before the first frame update void Start() { StartCoroutine("OpenCameraTest"); } IEnumerator OpenCameraTest() { // 申请相机权限 yield return Application.RequestUserAuthorization(UserAuthorization.WebCam); // 判断是否有相机权限 if (Application.HasUserAuthorization(UserAuthorization.WebCam)) { // 获取相机设备 WebCamDevice[] webCamDevices = WebCamTexture.devices; // 判断是否有相机设别 if (webCamDevices != null && webCamDevices.Length > 0) { // 把 0 号设备(移动端后置摄像头)名称赋值 string webCamName = webCamDevices[0].name; // 设置相机渲染宽高,并运行相机 webCamTexture = new WebCamTexture(webCamName, Screen.width, Screen.height); // webCamTexture.requestedFPS = 30; webCamTexture.Play(); // 把获取的图像渲染到画布上 rawImage.texture = webCamTexture; } } } }