Getting A Web Cam to Play on UI Texture Image

Alloha! :slight_smile:

I’m trying to figure out how to get a Web Cam to play on a UI image as I have WAY more control over layout, so far all I have been able to find is how to get a web cam to play on a plane.

Is there a way to get something like this onto a UI.Image that can be positioned onto a canvas?

It’s easy my friend. Use RawImage instead of Image. Add Unlit/Texture material to material property of RawImage. And use this code to play WebCam video on it:

public class PlayMovieTextureOnUI : MonoBehaviour 
{
	public RawImage rawimage;
	void Start () 
	{
		WebCamTexture webcamTexture = new WebCamTexture();
		rawimage.texture = webcamTexture;
		rawimage.material.mainTexture = webcamTexture;
		webcamTexture.Play();
	}
}


Hi @Max-Bot Would you possible be able to help with getting more than one web cam to be accessed?

UPDATE:
Never mind, I figured it out. I tweaked this code to also display a UI Text to display the current camera in use.

using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;

public class IDwebCams : MonoBehaviour {

	public RawImage rawimage;
	WebCamTexture webCamTexture;

	public Text webCamDisplayText;

	void Start ()
	{


		WebCamDevice[] cam_devices = WebCamTexture.devices;
		// for debugging purposes, prints available devices to the console
		for (int i = 0; i < cam_devices.Length; i++) 
		{
			print ("Webcam available: " + cam_devices *.name);*
  •  }*
    
  • }*

  • //CAMERA 01 SELECT*

  • public void GoWebCam01()*

  • {*

  •  WebCamDevice[] cam_devices = WebCamTexture.devices;*
    
  •  // for debugging purposes, prints available devices to the console*
    
  •  for (int i = 0; i < cam_devices.Length; i++)* 
    
  •  {*
    

print ("Webcam available: " + cam_devices .name);
* }*

* webCamTexture = new WebCamTexture(cam_devices[0].name, 480, 640, 30);
_
rawimage.texture = webCamTexture;_
_
if(webCamTexture != null)_
_
{_
_
webCamTexture.Play();_
_
Debug.Log("Web Cam Connected : "+webCamTexture.deviceName + "
");_
_
} _
webCamDisplayText.text = "Camera Type: " + cam_devices [0].name.ToString();
_
}_
_
//CAMERA 02 SELECT*_
* public void GoWebCam02()*
* {*
* WebCamDevice cam_devices = WebCamTexture.devices;
_
// for debugging purposes, prints available devices to the console*_
* for (int i = 0; i < cam_devices.Length; i++)
_
{_
print ("Webcam available: " + cam_devices .name);
_ }*_

* webCamTexture = new WebCamTexture(cam_devices[1].name, 480, 640, 30);
_ rawimage.texture = webCamTexture;
if(webCamTexture != null)
{
webCamTexture.Play();
Debug.Log("Web Cam Connected : "+webCamTexture.deviceName + "
");
}_
webCamDisplayText.text = "Camera Type: " + cam_devices [1].name.ToString();
_ }
}*_

Hi,

Just want to add that, it will not work when level is reloaded. For it to work you need to stop it before reloading level by using below code line:

webcamTexture.Stop();

It seems WebCamTexture can only detect physical webcams, but how to do you make it use virtual webcams?

I want to use OBS (Welcome | OBS) or any other “virtual camera” video stream, how can you select this?

hi, @Max-Bot I am getting an orange screen when I run the program in unity. Am I missing something