using UnityEngine.UI;
using UnityEngine;
public class WebCam : MonoBehaviour
{
///
/// Index of the current camera
///
protected int camdex = 0;
///
/// Texture to paste on the display
///
protected WebCamTexture tex;
///
/// Display for the video feed
///
public RawImage display;
///
/// Setup the webcam correctly
///
void Awake()
{
WebCamDevice device = WebCamTexture.devices[camdex];
tex = new WebCamTexture(device.name);
display.texture = tex;
tex.Play();
}
///
/// Function to toggle between stopping and starting
///
/*
public void toggle()
{
if (tex.isPlaying)
{
tex.Stop();
}
else
{
tex.Play();
}
}
*/
public void PlayCam()
{
if (!tex.isPlaying) tex.Play();
}
public void StopCam()
{
if (tex.isPlaying) tex.Stop();
}
///
/// Swap webcam by cycling through the `WebCamTexture.devices` list
///
public void SwapCam()
{
if (WebCamTexture.devices.Length > 0)
{
// Stop the old camera
display.texture = null;
tex.Stop();
tex = null;
// Find the new camera
camdex += 1;
camdex %= WebCamTexture.devices.Length;
// Start the new camera
WebCamDevice device = WebCamTexture.devices[camdex];
tex = new WebCamTexture(device.name);
display.texture = tex;
tex.Play();
}
}
///
/// Scene changing is implemented here to avoid problems with webcam
///
public void GotoThemeSelection()
{
display.texture = null;
tex.Stop();
tex = null;
SystemController.GetInstance().BackToPreviousScene();
}
}