using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(RawImage))] //脚本挂在RawImage
public class CameraDevice : MonoBehaviour
{
private List<WebCamTexture> webCamTextures = new List<WebCamTexture>();
private RawImage image;
#region Unity Method
// Called once before Start
void Awake()
{
image = GetComponent<RawImage>();
}
// Use this for initialization
IEnumerator Start()
{
//获取授权
yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
if (Application.HasUserAuthorization(UserAuthorization.WebCam))
{
WebCamDevice[] devices = WebCamTexture.devices;
for (int i = 0; i < devices.Length; i++)
{
WebCamTexture webCamTexture = new WebCamTexture(devices[i].name, 400, 300, 12);
webCamTexture.Play();
webCamTextures.Add(webCamTexture);
}
}
}
// Update is called once per frame
void Update()
{
}
// OnDestroy is called when the App is killed
void OnDestroy()
{
}
#endregion
public void SetDevice(int index) //按钮调用
{
image.texture = webCamTextures[index];
}
}