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