1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- 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<GameObject> mats=new List<GameObject>();
- 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<MeshRenderer>();
- RawImage raw = go.GetComponent<RawImage>();
-
- 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();//开始摄像
- }
- }
- }
|