【问题标题】:Display live camera feed in Unity在 Unity 中显示实时摄像机源
【发布时间】:2013-10-20 20:28:27
【问题描述】:

我有一个关于 Unity 的问题。我希望这个问题以前没有得到回答。 我想将一台摄像机(如高清摄像机)连接到我的计算机,并且视频源应显示在我的 Unity 场景中。可以把它想象成一个虚拟电视屏幕,它可以实时显示摄像机所看到的内容。我怎样才能做到这一点?谷歌没有为我指明正确的方向,但也许我只是无法正确查询;)

我希望你明白我的目的。

【问题讨论】:

  • 我没有使用 Unity,但我想到的是从相机获取原始图像,然后将其映射到显示在四边形上的纹理。
  • 雨果说的。一些插件,如 Prime 31 为直播提供支持(对于 $$$)。或者你可以使用 Unity 的webcamtexture

标签: c# unity3d unityscript


【解决方案1】:

是的,这当然是可能的,幸运的是,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();

【讨论】:

  • 我不得不通过更新 WebCamTexture 来稍微修改一下,像这样:WebCamTexture webcam = new WebCamTexture("NameOfDevice");
【解决方案2】:

如果有帮助,我将根据上面接受的答案发布一个答案,以 C# 脚本编写(接受的答案是 JavaScript)。只需将此脚本附加到附加了渲染器的游戏对象上,它就可以工作了。

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 的示例,我制作了一个使用原始图像播放网络摄像头纹理的示例,以便您可以将网络摄像头添加到 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);
            }
    
            //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 之一,您将看到gui information panel 上出现一个“原始图像”表单字段GameObject ,您可以将UI RawImage GameObject 拖放到表单字段中。

    【讨论】:

      【解决方案4】:

      我不得不修改@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&lt;RawImage&gt;();
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-30
      • 2021-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-12
      相关资源
      最近更新 更多