當(dāng)前位置:
首頁 >
Unity获取摄像头权限
發(fā)布時(shí)間:2023/12/14
38
豆豆
生活随笔
收集整理的這篇文章主要介紹了
Unity获取摄像头权限
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
直接上代碼,unity幫我們內(nèi)置了功能。
using System.Collections; using UnityEngine; using UnityEngine.UI;public class Test : MonoBehaviour {// 圖片組件public RawImage rawImage;//圖形組件父實(shí)體public RectTransform imageParent;//當(dāng)前相機(jī)索引private int index = 0;//當(dāng)前運(yùn)行的相機(jī)private WebCamTexture currentWebCam;void Start(){StartCoroutine(Call());}public IEnumerator Call(){// 請求權(quán)限yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);if (Application.HasUserAuthorization(UserAuthorization.WebCam) && WebCamTexture.devices.Length > 0){// 創(chuàng)建相機(jī)貼圖currentWebCam = new WebCamTexture(WebCamTexture.devices[index].name, Screen.width, Screen.height, 60);rawImage.texture = currentWebCam;currentWebCam.Play();//前置后置攝像頭需要旋轉(zhuǎn)一定角度,否則畫面是不正確的,必須置于Play()函數(shù)后rawImage.rectTransform.localEulerAngles = new Vector3(0, 0, -currentWebCam.videoRotationAngle);}}//切換前后攝像頭public void SwitchCamera(){if (WebCamTexture.devices.Length < 1)return;if (currentWebCam != null)currentWebCam.Stop();index++;index = index % WebCamTexture.devices.Length;// 創(chuàng)建相機(jī)貼圖currentWebCam = new WebCamTexture(WebCamTexture.devices[index].name, Screen.width, Screen.height, 60);rawImage.texture = currentWebCam;currentWebCam.Play();//前置后置攝像頭需要旋轉(zhuǎn)一定角度,否則畫面是不正確的,必須置于Play()函數(shù)后rawImage.rectTransform.localEulerAngles = new Vector3(0, 0, -currentWebCam.videoRotationAngle);}}?
總結(jié)
以上是生活随笔為你收集整理的Unity获取摄像头权限的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 天猫HTMl静态页面
- 下一篇: 扫描MAC地址脚本--保存