using TMPro; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; /// /// This class is dedicated to the camera and other actions that later could be connected to it, like the feedback. /// It is responsible for finding working cameras, displaying them and being able to toggle between them. /// This class also holds temporary code to display feedback via a button. /// public class Webcam : MonoBehaviour { /// /// Index to indicate which camera is being used /// int camdex = 0; /// /// This texture is used as an intermidiary between the camera output and the display image /// WebCamTexture tex; /// /// Reference to the RawImage that will display the video /// public RawImage display; /// /// Reference to the button that is currently used to test the feedback-display /// public Button feedback; /// /// This is a reference to the PANEL that holds the feedbackwindow /// public GameObject popup; /// /// This is a reference to the textfield that holds the part of the feedback-window that will change: bad/good/excellent /// public TextMeshProUGUI dynamic; /// /// This function is called at the start of the frame. /// It inactivatis the popup, finds a webcam to use and links it via the WebcamTexture to the display RawImage. /// void Awake() { popup.SetActive(false); if (WebCamTexture.devices.Length > 0) { WebCamDevice device = WebCamTexture.devices[camdex]; tex = new WebCamTexture(device.name); display.texture = tex; tex.Play(); } } /// /// So long as there are cameras to use, you swap the camera you are using to another in the list. /// public void SwapCam() { if (WebCamTexture.devices.Length > 0) { // Stop the old camera // If there was no camera playing before, then you dont have to reset the texture, as it wasn't assigned in the first place. if (tex.isPlaying) { 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(); } } /// /// The notmal sceneChanger cannot be used here since the camera also needs to be stopped. /// This extra functionality is implemented in this function /// /// The path for the scene you want to travel to, assuming root-directory is Assets public void LoadScene(string sceneName) { display.texture = null; tex.Stop(); tex = null; SceneManager.LoadScene(sceneName); } /// /// This function toggles between inactivity and activity for the popup panel. /// This will be changed later when the model gets integrated, probably being timed to dissapear. /// public void Show_feedback() { if (popup.activeSelf) { dynamic.text = ""; popup.SetActive(false); return; } double index = UnityEngine.Random.value; if (index < 0.5) { dynamic.text = "Poor"; } else if (index > 0.8) { dynamic.text = "Excellent"; } else { dynamic.text = "Good"; } popup.SetActive(true); } }