Display camera live stream in Unity

I have a question about Unity. I hope this has not been answered before. I want to connect a camera (such as an HD camera) to a computer, and the video channel should be displayed inside my Unity scene. Think of it as a virtual TV screen that displays what the camera sees in real time. How can i do this? Google didn't point me in the right direction, but maybe I just can't get the request correctly;)

I hope you understand what I'm going for.

+6
source share
3 answers

Yes, of course, it is possible, and fortunately for you, Unity3D really supports it pretty well out of the box. You can use WebCamTexture to find the webcam and make it texture. From there, you can display the texture on anything in a 3D scene, including, of course, the screen of your virtual television.

It looks pretty straightforward, but the code below should start with you.

List and print the connected devices that it detects:

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

Connect to the attached webcam and send the image data to the texture:

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

In case this helps, I send the answer based on the accepted answer above, written as a C # script (the accepted answer was in JavaScript). Just attach this script to GameObject with attached visualizer and it should work.

 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(); } } 
+2
source

It seems that WebCamTexture can only detect physical webcams, but how to get it to use virtual webcams?

I want to use OBS ( https://obsproject.com/welcome ) or any other video stream with a "virtual camera", how can you choose this?

0
source

Source: https://habr.com/ru/post/956366/


All Articles