直接上代码,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.localEu