在Unity中显示实时摄像头源
我有关于Unity的问题。 我希望以前没有回答过。 我想将相机(如高清摄像头)连接到我的电脑,video输入应显示在Unity场景中。 可以把它想象成一个虚拟电视屏幕,显示相机实时看到的内容。 我怎样才能做到这一点? 谷歌没有指出我正确的方向,但也许我只是无法正确查询;)
我希望你明白我的意思。
是的,这当然是可能的,幸运的是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();
如果它有帮助,我会根据上面接受的答案发布一个答案,写成一个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(); // assuming the first available WebCam is desired WebCamTexture tex = new WebCamTexture(devices[0].name); rend.material.mainTexture = tex; tex.Play(); } }