素材巴巴 > 程序开发 >

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

程序开发 2023-09-13 20:05:27

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);}
 }
 

 

 


标签:

素材巴巴 Copyright © 2013-2021 http://www.sucaibaba.com/. Some Rights Reserved. 备案号:备案中。