1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > Unity调用安卓(Android)手机摄像头

Unity调用安卓(Android)手机摄像头

时间:2018-09-22 02:05:13

相关推荐

Unity调用安卓(Android)手机摄像头

using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI;public class UITest : 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);}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。