unity打开外部相机的画面给到image上
代码挂哪里都可以
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class CameraTest : MonoBehaviour
{
WebCamTexture camTexture;
public Image img;//用来显示画面的ui
public void Update()
{
if (Application.HasUserAuthorization(UserAuthorization.WebCam))
{
WebCamDevice[] cameraDevices = WebCamTexture.devices;
string deviceName = cameraDevices[0].name;
camTexture = new WebCamTexture(deviceName,1280,720,30);
img.material.mainTexture = camTexture;
if (!camTexture.isPlaying)
camTexture.Play();
}
}
}
首先场景中建一个Panel
新建一个material给Panel
shader选择(这个随意)
把前边建好的Panel拖到脚本上
运行