using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class openCamera : MonoBehaviour { [HideInInspector] public WebCamTexture webTex; [HideInInspector] public string deviceName; public static openCamera Instance; private static List mats=new List(); void Start() { if (Instance!=null) { webTex = openCamera.Instance.webTex; setTexture(this.gameObject, webTex); mats.Add(this.gameObject); } else { Screen.sleepTimeout = SleepTimeout.NeverSleep; Instance = this; mats.Add(this.gameObject); StartCoroutine(CallCamera()); } } public void setTexture(GameObject go,Texture tx) { MeshRenderer mr = go.GetComponent(); RawImage raw = go.GetComponent(); if(mr ==null) { raw.texture = tx; } else { mr.material.mainTexture = tx; } } public void restart(int fps=30) { CustomInfo.FPS = fps; StartCoroutine(CallCamera()); } void Update() { } /// ///调用摄像头 /// IEnumerator CallCamera() { yield return Application.RequestUserAuthorization(UserAuthorization.WebCam); if (Application.HasUserAuthorization(UserAuthorization.WebCam)) { WebCamDevice[] devices = WebCamTexture.devices; deviceName = devices[0].name; if (webTex != null) { webTex.Stop(); webTex = null; } //设置摄像机摄像的区域 webTex = new WebCamTexture(deviceName, 1920,1440, CustomInfo.FPS); for (int i = 0; i < mats.Count; i++) { setTexture(mats[i], webTex); } webTex.Play();//开始摄像 } } }