Files
unity-application/Assets/SpellingBee/Scripts/SpellingBeeWebcam.cs
2023-03-13 21:49:40 +00:00

77 lines
1.7 KiB
C#

using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class SpellingBeeWebcam : MonoBehaviour
{
/// <summary>
/// Index of the current camera
/// </summary>
int camdex = 0;
/// <summary>
/// Texture to paste on the display
/// </summary>
WebCamTexture tex;
/// <summary>
/// Display for the video feed
/// </summary>
public RawImage display;
/// <summary>
/// For later use
/// </summary>
public TextMeshProUGUI dynamic;
/// <summary>
/// Initialize the camera view
/// </summary>
void Awake()
{
WebCamDevice device = WebCamTexture.devices[camdex];
tex = new WebCamTexture(device.name);
display.texture = tex;
tex.Play();
}
/// <summary>
/// Switch to a different webcam
/// </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 LoadScene(string sceneName)
{
display.texture = null;
tex.Stop();
tex = null;
SceneManager.LoadScene(sceneName);
}
}