using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ARCanvasTest : MonoBehaviour
{
public Image image;
void Start()
{
StartCoroutine (RequstCamera ());
}
IEnumerator RequstCamera()
{
//协程返回用户权限
yield return Application.RequestUserAuthorization (UserAuthorization.WebCam);
//如果用户有权限
if (Application.HasUserAuthorization(UserAuthorization.WebCam)) {
//获取所有的摄像头设备
WebCamDevice[] device = WebCamTexture.devices;
//记录摄像头的下标
int index = -1;
for (int i = 0; i < device.Length; i++) {
//如果是后置摄像头
if (!device[i].isFrontFacing) {
index = i;
break;
}
}
//根据摄像头的名字,创建一个摄像机图像,展示到plane上
WebCamTexture texture = new WebCamTexture (device[index].name);
//将图像展示到image上
image.canvasRenderer.SetTexture (texture);
//启动摄像头
texture.Play();
}
}
}
网友评论