Files
unity-application/Assets/Hangman/Scripts/WebCam.cs
2023-03-19 23:02:50 +00:00

96 lines
2.0 KiB
C#

using UnityEngine.UI;
using UnityEngine;
public class WebCam : MonoBehaviour
{
/// <summary>
/// Index of the current camera
/// </summary>
protected int camdex = 0;
/// <summary>
/// Texture to paste on the display
/// </summary>
protected WebCamTexture tex;
/// <summary>
/// Display for the video feed
/// </summary>
public RawImage display;
/// <summary>
/// Setup the webcam correctly
/// </summary>
void Awake()
{
WebCamDevice device = WebCamTexture.devices[camdex];
tex = new WebCamTexture(device.name);
display.texture = tex;
tex.Play();
}
/// <summary>
/// Function to toggle between stopping and starting
/// </summary>
/*
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();
}
/// <summary>
/// Swap webcam by cycling through the `WebCamTexture.devices` list
/// </summary>
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();
}
}
/// <summary>
/// Scene changing is implemented here to avoid problems with webcam
/// </summary>
public void GotoThemeSelection()
{
display.texture = null;
tex.Stop();
tex = null;
SystemController.GetInstance().BackToPreviousScene();
}
}