- Home /
Getting A Web Cam to Play on UI Texture Image
Alloha! :)
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?
Can you tell me how to save this on Android and iOS?
Thank you
NOTE as I mentioned below there seems to be a critical new asset on the assetstore ... https://www.assetstore.unity3d.com/en/#!/content/52154
It seems WebCamTexture can only detect physical webcams, but how to do you make it use virtual webcams?
I want to use OBS (https://obsproject.com/welcome) or any other "virtual camera" video stream, how can you select this?
Answer by Max-Bot · Feb 25, 2015 at 07:38 PM
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();
}
}
And for those who miss the header:
using UnityEngine.UI;
THAN$$anonymous$$ YOU SO $$anonymous$$UCH !!!! PERFECT SOLUTION !
This solution above doesn't work for me... im not sure why, i get this error: Assets/Play$$anonymous$$ovieTextureOnUI.cs(4,37): error CS0246: The type or namespace name `$$anonymous$$onoBehaviour' could not be found. Are you missing a using directive or an assembly reference?
Hi all, Do you know if the issue with "WebCamTexture breaking Android device camera autofocus" is solved yet? I am working on a project that does not allow the use of stuff from the AssetStore. Android Auto focus stopped working properly after creating a WebCamTexture and I cannot seem to find a way to control the focus of an Android camera through Unity, so it seems the only way to resolve this is to write my own plugin in Java, which will set auto-focus and then call it from Unity? Is this really the only way?
Answer by KnightRiderGuy · Dec 19, 2017 at 03:09 PM
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 [i].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 [i].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 + "\n");
}
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 [i].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 + "\n");
}
webCamDisplayText.text = "Camera Type: " + cam_devices [1].name.ToString();
}
}
On an important note: I did notice on my $$anonymous$$ac I did not have to stop texture from playing, but on my PC going back and forth between the two different camera would only work the one time. the way around this was to make each button run a short coroutine where first the web cam textures stops playing, then waits for about half a second before loading the 2nd camera and playing the webcamtexture again. Doing this for each button on the PC allowed for transition back and forth between camera views. Tested in a stand alone build to and it works fine ;)
Answer by mehtanitish · Aug 28, 2018 at 11:20 AM
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();
Answer by akilesh0312 · May 03, 2020 at 10:11 PM
hi, @Max-Bot I am getting an orange screen when I run the program in unity. Am I missing something
Your answer
Follow this Question
Related Questions
How to fit Canvas to all resolution ? 1 Answer
Dynamically setting gameobject sprites in UI Image 0 Answers
Re-center UI Image after zooming out 0 Answers
ui button in world space not working 0 Answers
Clamp UI Image to Canvas 0 Answers