12

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 ;)

I hope you understand what I'm going for.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
Sören Kampschroer
  • 348
  • 1
  • 3
  • 9
  • 1
    I haven't used Unity, but what comes to mind is getting the raw image from the camera and then mapping this to a texture displayed on a quad. – Hugo Tunius Oct 20 '13 at 20:41
  • 1
    What Hugo said. Some plugins, like [Prime 31](http://prime31.com/plugins) provide support (for $$$) for live streaming. Or you can use Unity's [webcamtexture](http://docs.unity3d.com/Documentation/ScriptReference/WebCamTexture.html) – Jerdak Oct 21 '13 at 04:06

4 Answers4

23

Yes that certainly is possible and luckily for you Unity3D actually supports it quite well out of the box. You can use a WebCamTexture to 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.

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();
S.Richmond
  • 11,412
  • 6
  • 39
  • 57
  • 1
    I had to modify slightly by newing up the WebCamTexture, like this: `WebCamTexture webcam = new WebCamTexture("NameOfDevice");` – Feckmore May 04 '15 at 14:21
9

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.

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();
    }
}
Stemkoski
  • 8,936
  • 3
  • 47
  • 61
3

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.

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.

Jacksonkr
  • 31,583
  • 39
  • 180
  • 284
0

I had to modify the raw image code of @Jachsonkr to make it work for me:

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.

Andi Schroff
  • 1,156
  • 1
  • 12
  • 18
  • If you are using the panel to add RawImage, should still get the component with script. `RawImage m_RawImage;` `m_RawImage = GetComponent();` – Expert Ngobeni Aug 29 '21 at 13:03