C# 在 Unity 中显示实时摄像机源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19482481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display live camera feed in Unity
提问by S?ren Kampschroer
I have a question regarding Unity. I hope this hasn't been answered before. I want to connect a Camera (like a HD cam) to my computer and the video feed should be displayed inside my Unity scene. Think of it like a virtual television screen, that displays what the camera is seeing in realtime. How can i do this? Google didn't point me in the right direction, but maybe I'm just unable to get the query right ;)
我有一个关于 Unity 的问题。我希望以前没有回答过这个问题。我想将相机(如高清摄像头)连接到我的计算机,视频源应该显示在我的 Unity 场景中。把它想象成一个虚拟的电视屏幕,它实时显示相机所看到的内容。我怎样才能做到这一点?谷歌没有给我指明正确的方向,但也许我只是无法正确查询;)
I hope you understand what I'm going for.
我希望你明白我要做什么。
采纳答案by S.Richmond
Yes that certainly is possible and luckily for you Unity3D actually supports it quite well out of the box. You can use a WebCamTextureto find the webcam and render it to a texture. From there you can choose to render the texture on anything in the 3D scene, including your virtual television screen of course.
是的,这当然是可能的,幸运的是 Unity3D 实际上开箱即用地支持它。您可以使用WebCamTexture来查找网络摄像头并将其渲染为纹理。从那里你可以选择在 3D 场景中的任何东西上渲染纹理,当然包括你的虚拟电视屏幕。
It looks pretty self explanatory but the below code should start you off.
它看起来很自我解释,但下面的代码应该让你开始。
List and print out the connected devices it detects:
列出并打印出它检测到的连接设备:
var devices : WebCamDevice[] = WebCamTexture.devices;
for( var i = 0 ; i < devices.length ; i++ )
Debug.Log(devices[i].name);
Connect to an attached webcam and send the image data to a texture:
连接到附加的网络摄像头并将图像数据发送到纹理:
WebCamTexture webcam = WebCamTexture("NameOfDevice");
renderer.material.mainTexture = webcam;
webcam.Play();
回答by Lee Stemkoski
In case it helps, I'm posting an answer, based on the accepted answer above, written as a C# script (the accepted answer was in JavaScript). Just attach this script to a GameObject that has a renderer attached, and it should work.
如果有帮助,我将根据上面接受的答案发布一个答案,编写为 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();
}
}
回答by Hymansonkr
Working from @LeeStemKoski 's example I made an example that uses a raw image to play the webcam texture so you can add the webcam to a UI.
根据@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();
}
}
** EDIT **
** 编辑 **
This is pretty self-explanatory but just in case: attach this script to one of your GameObject
's and you'll see a "Raw Image" form field show up on the gui information panel
for that GameObject
, you can drag and drop your UI RawImage GameObject
into the form field.
这是不言自明,但以防万一:重视这个脚本到你的一个GameObject
的,你会看到一个‘原始图像’表单字段中显示的gui information panel
为GameObject
,你可以拖放UI RawImage GameObject
到表单字段。
回答by Andi Schroff
I had to modify the raw image code of @Jachsonkr to make it work for me:
我必须修改@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();
}
}
I have added a raw image (which always in added to a panel). Then simply added this code to the raw image via drag&drop and the webcam was displayed.
我添加了一个原始图像(它总是添加到面板中)。然后只需通过拖放将此代码添加到原始图像中即可显示网络摄像头。