Unity开启摄像头,在UI上绘制出实时的摄像头数据流。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CameraCall : MonoBehaviour
{
WebCamTexture tex;
public RawImage rawImgl;
void Start()
{
StartCoroutine(OpenCamera());
}
IEnumerator OpenCamera()
{
yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
if (Application.HasUserAuthorization(UserAuthorization.WebCam))
{
WebCamDevice[] device = WebCamTexture.devices;
string deviceName = device[0].name;
tex = new WebCamTexture(deviceName, 1920, 1080);
rawImgl.texture = tex;
tex.Play();
//如果想获取摄像头的分辨率,需要在它play后才能获取。否则值是错误的
//Debug.Log(camTexture.width + "dddddd" + camTexture.height);
}
}
void StopCamera()
{
if (Application.HasUserAuthorization(UserAuthorization.WebCam))
{
WebCamDevice[] device = WebCamTexture.devices;
string deviceName = device[0].name;
tex.Stop();
}
}
}