using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class OpenCamera : MonoBehaviour
{
public RawImage tt;//相机渲染的UI
private WebCamTexture webCam;
void Start()
{
OpenCameraBackground();
}
/// <summary>
/// 打开摄像机
/// </summary>
public void OpenCameraBackground()
{
StartCoroutine("StartCam");
}
public IEnumerator StartCam()
{
int maxl = Screen.width;
if (Screen.height > Screen.width)
{
maxl = Screen.height;
}
yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
if (Application.HasUserAuthorization(UserAuthorization.WebCam))
{
if (webCam != null)
{
webCam.Stop();
}
tt.gameObject.SetActive(true);//打开渲染图
WebCamDevice[] devices = WebCamTexture.devices;//获取可用设备
string devicename = devices[0].name;
webCam = new WebCamTexture(devicename, maxl, maxl, 12)
{
wrapMode = TextureWrapMode.Repeat
};
tt.texture = webCam;
webCam.Play();
}
}
}
Unity 开启外部摄像头并渲染到UI上
猜你喜欢
转载自blog.csdn.net/qq_42047805/article/details/100122170
今日推荐
周排行