C# 在Unity中显示实时摄影机馈送

C# 在Unity中显示实时摄影机馈送,c#,unity3d,unityscript,C#,Unity3d,Unityscript,我有一个关于团结的问题。我希望这个问题以前没有得到回答。 我想将摄像头(如高清摄像头)连接到我的电脑,视频提要应该显示在我的Unity场景中。把它想象成一个虚拟电视屏幕,实时显示摄像机所看到的东西。我该怎么做?谷歌没有给我指明正确的方向,但也许我只是无法正确地查询;) 我希望您理解我的意图。是的,这当然是可能的,幸运的是,Unity3D实际上在开箱即用的情况下支持它。您可以使用查找网络摄像头并将其渲染为纹理。从那里你可以选择在3D场景中的任何东西上渲染纹理,当然包括你的虚拟电视屏幕 这看起来很简

我有一个关于团结的问题。我希望这个问题以前没有得到回答。 我想将摄像头(如高清摄像头)连接到我的电脑,视频提要应该显示在我的Unity场景中。把它想象成一个虚拟电视屏幕,实时显示摄像机所看到的东西。我该怎么做?谷歌没有给我指明正确的方向,但也许我只是无法正确地查询;)


我希望您理解我的意图。

是的,这当然是可能的,幸运的是,Unity3D实际上在开箱即用的情况下支持它。您可以使用查找网络摄像头并将其渲染为纹理。从那里你可以选择在3D场景中的任何东西上渲染纹理,当然包括你的虚拟电视屏幕

这看起来很简单,但下面的代码应该会让你开始

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

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

如果有帮助的话,我将根据上面接受的答案发布一个答案,以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();
    }
}
public-class-DisplayWebCam:MonoBehavior
{
无效开始()
{
WebCamDevice[]设备=WebCamTexture.devices;
//出于调试目的,将可用设备打印到控制台
对于(int i=0;i
根据@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();
    }
}
public-class-DisplayWebCam:MonoBehavior
{
[序列化字段]
私有UnityEngine.UI.RawImage\u RawImage;
void Start()
{
WebCamDevice[]设备=WebCamTexture.devices;
//出于调试目的,将可用设备打印到控制台
对于(int i=0;i
**编辑**


这是很自然的解释,但只是以防万一:将此脚本附加到您的一个
游戏对象
,您将看到该
游戏对象的
gui信息面板
上显示一个“原始图像”表单字段,您可以将您的
UI RawImage游戏对象拖放到表单字段中。

我必须修改@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();
  }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
公共类显示网络摄像头:MonoBehavior
{
[序列化字段]
私有UnityEngine.UI.RawImage\u RawImage;
void Start()
{
WebCamDevice[]设备=WebCamTexture.devices;
//出于调试目的,将可用设备打印到控制台
对于(int i=0;i

我已经添加了一个原始图像(总是添加到面板中)。然后简单地通过拖放将此代码添加到原始图像中,并显示网络摄像头。

我没有使用Unity,但我想到的是从摄像头获取原始图像,然后将其映射到显示在四边形上的纹理。Hugo如是说。一些插件,如为实时流媒体提供支持(对于$$$)。或者,您可以使用Unity的,我必须通过更新WebCamTexture来稍微修改,如下所示:
WebCamTexture webcam=new WebCamTexture(“NameOfDevice”)
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();
  }
}