1.WebCamTexture
首先了解下WebCamTexture这个类,通过调用个这个类可以获取到可以使用的摄像头。
通过使用该方法我们可以获取他返回的设备名
2.RequestUserAuthorization
由于此过程是一个异步方法,所以调用的时候我们采用协程的方法来实现。
有了以上的信息,就可以让unity调用外部的摄像头了。
public class Test : MonoBehaviour
{
public RawImage rawImage;
WebCamTexture tex;
IEnumerator Start()
{
yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
if (Application.HasUserAuthorization(UserAuthorization.WebCam))
{
WebCamDevice[] devices = WebCamTexture.devices;
deviceName = devices[0].name;
tex = new WebCamTexture(deviceName, 1920, 1080, 30);
rawImage.texture= tex;
tex.Play();
}
}
}
因为WebCamTexture属于UnityEngine,所以不需要多余的using。
上面的代码是获取一个默认的摄像头,如果有多个摄像头,但是要获取指定的摄像头的话,采用下面的代码。
String devicename=”Logitech HD Pro Webcam C920”;
for(int i=0;i<devices.length;++i)
{
if(devicename==devices[i].name)
{
tex = new WebCamTexture(deviceName, 1920, 1080, 30);
}
}