在Unity中显示实时摄像头视频流

12

我有一个关于Unity的问题。希望这之前没有被回答过。 我想要将一台相机(例如高清摄像机)连接到我的计算机上,视频内容应该在我的Unity场景中显示。类似于虚拟电视屏幕,实时显示相机正在拍摄的内容。如何做到这一点?谷歌没有指向正确的方向,但也许我只是不能正确地查询 ;)

希望您明白我的意思。


1
我没有使用过Unity,但我想到的是从相机获取原始图像,然后将其映射到在四边形上显示的纹理。 - Hugo Tunius
1
像Hugo所说的那样。一些插件,比如Prime 31,提供了直播支持(需要$$$)。或者你可以使用Unity的webcamtexture - Jerdak
4个回答

23

是的,这是完全可能的,幸运的是,Unity3D实际上非常支持这一点。您可以使用WebCamTexture查找摄像头并将其渲染到纹理上。从那里,您可以选择在3D场景中的任何物体上呈现纹理,当然包括您的虚拟电视屏幕。

它看起来相当简单,但以下代码应该让您开始。

列出并打印出它检测到的连接设备:

var devices : WebCamDevice[] = WebCamTexture.devices;
for( var i = 0 ; i < devices.length ; i++ )
    Debug.Log(devices[i].name);

连接到已连接的网络摄像头并将图像数据发送到纹理:

WebCamTexture webcam = WebCamTexture("NameOfDevice");
renderer.material.mainTexture = webcam;
webcam.Play();

1
我不得不进行一些修改,通过新建WebCamTexture对象来实现,像这样:WebCamTexture webcam = new WebCamTexture("NameOfDevice"); - Feckmore

9

如果有帮助的话,我会根据上面被接受的答案编写一个基于C#脚本的答案(被接受的答案是JavaScript编写的)。只需将此脚本附加到已附加了渲染器的GameObject上,它就应该可以工作。

public class DisplayWebCam : MonoBehaviour
{
    void Start ()
    {
        WebCamDevice[] devices = WebCamTexture.devices;

        // for debugging purposes, prints available devices to the console
        for(int i = 0; i < devices.Length; i++)
        {
            print("Webcam available: " + devices[i].name);
        }

        Renderer rend = this.GetComponentInChildren<Renderer>();

        // assuming the first available WebCam is desired
        WebCamTexture tex = new WebCamTexture(devices[0].name);
        rend.material.mainTexture = tex;
        tex.Play();
    }
}

3

参照 @LeeStemKoski 的示例,我制作了一个使用原始图像播放网络摄像头纹理的示例,以便您可以将网络摄像头添加到用户界面中。

public class DisplayWebCam : MonoBehaviour
{
    [SerializeField]
    private UnityEngine.UI.RawImage _rawImage;

    void Start()
    {
        WebCamDevice[] devices = WebCamTexture.devices;

        // for debugging purposes, prints available devices to the console
        for (int i = 0; i < devices.Length; i++)
        {
            print("Webcam available: " + devices[i].name);
        }

        //Renderer rend = this.GetComponentInChildren<Renderer>();

        // assuming the first available WebCam is desired

        WebCamTexture tex = new WebCamTexture(devices[0].name);
        //rend.material.mainTexture = tex;
        this._rawImage.texture = tex;
        tex.Play();
    }
}

**编辑**

这很容易理解,但以防万一:将此脚本附加到您的一个 GameObject 中,您将在该 GameObjectgui信息面板 上看到一个“原始图像”表单字段,您可以将您的 UI RawImage GameObject 拖放到表单字段中。


0
我不得不修改@Jachsonkr的原始图像代码,以使其适用于我:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class DisplayWebCam : MonoBehaviour
{
  [SerializeField]
  private UnityEngine.UI.RawImage _rawImage;

  void Start()
  {
      WebCamDevice[] devices = WebCamTexture.devices;

      // for debugging purposes, prints available devices to the console
      for (int i = 0; i < devices.Length; i++)
      {
          print("Webcam available: " + devices[i].name);
      }

      WebCamTexture tex = new WebCamTexture(devices[0].name);

      RawImage m_RawImage;
      m_RawImage = GetComponent<RawImage>();
      m_RawImage.texture = tex;
      tex.Play();
  }
}

我添加了一张原始图片(总是添加到一个面板中)。然后通过拖放方式将此代码添加到原始图像中,网络摄像头就会显示出来。


如果您使用面板来添加 RawImage,则仍应该使用脚本获取该组件。RawImage m_RawImage; m_RawImage = GetComponent<RawImage>(); - Expert Ngobeni

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接