Unity中连接多个摄像头时选择一个摄像头并输出视频

问题描述

我正在尝试通过在 Unity 中连接手部跟踪摄像头设备(Leap motion:窗口设备管理器列表:0)和网络摄像头(窗口设备管理器列表:1)来显示 AR 视频。

Leap motion 使用制造商的 SDK 和包文件进行连接,因此无需额外编码即可在 Unity 中连接相机。

当我在 Unity 中连接网络摄像头(窗口设备管理器列表:1)并显示 AR 视频时出现问题。

当下面的代码应用到一个物体上时,如果 Leap motion 和 webcam camera 都连接了,leap motion 被识别并输出为视频,webcam camera 的视频输出变得不可能。

如果从PC上拔下leap motion后只连接网络摄像头,网络摄像头的视频输出是可能的。

我想通过在 Leap motion 和网络摄像头连接到 PC 的对象上选择网络摄像头(窗口设备管理器列表:1)来输出视频。

由于我是Unity的初学者,我只需在下面的代码修改即可。

等待帮助。

using UnityEngine;
using System.Collections;
 
public class WebCam : MonoBehavIoUr {
 
    // Use this for initialization
    void Start () {
        WebCamTexture web = new WebCamTexture(1280,720,60);
        GetComponent<MeshRenderer>().material.mainTexture = web;
        web.Play();
    }
    
    // Update is called once per frame
    void Update () {
    
    }
}

解决方法

有一个 WebCamTexture 的构造函数接受参数

deviceName:要使用的视频输入设备的名称。

您可以通过 WebCamTexture.devices 列出所有可用设备并获取名称,例如

var devices = WebCamTexture.devices;
var webcamTexture = new WebCamTexture(devices[1].name);

然后您也可以过滤掉您需要的设备,例如

using System.Linq;

...

var device = devices.Select(d => d.name).FirstOrDefault(n => !n.Contains("Leap"));

要了解如何调用相机并能够按名称过滤,您可以将它们全部打印出来,例如

Debug.Log(string.Join("\n",devices.Select(d => d.name)));

理论上,您甚至可以将它们放入下拉列表中,让用户在创建 WebCamTexture 之前决定要使用的设备,这样您就完全不必猜测硬编码的名称了 ;)


另请注意:

在创建 WebCamTexture 之前调用 Application.RequestUserAuthorization