using UnityEngine;
using System.Collections;
public class OpenCamera : MonoBehaviour {
public WebCamTexture tex;
private string deviceName="";
// Use this for initialization
void Start () {
StartCoroutine(openCam());
}
IEnumerator openCam()
{
yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);//授权
if (Application.HasUserAuthorization(UserAuthorization.WebCam))
{
WebCamDevice[] devices = WebCamTexture.devices;
deviceName = devices[0].name;
print(deviceName);
//设置摄像机摄像的区域
tex = new WebCamTexture(deviceName, 400, 300, 12);
GetComponent<Renderer>().material.mainTexture = tex;///材质赋值不能在外面赋值,不然画面传不进来
tex.Play();//开始摄像
}
}
}