ZoeAppClass/Assets/Scripts/Class/Question/QuestOptionControl.cs

142 lines
3.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class QuestOptionControl : MonoBehaviour
{
bool isRight = false;
GameObject border;
public int answersID;
private void Start()
{
GameObject obj = transform.GetChild(1).gameObject;
border = obj;
obj.SetActive(false);
}
public void SetRightQuestion(bool teacher) {
isRight = true;
if (teacher)
{
Text txt = GetComponentInChildren<Text>();
txt.color = new Color(0, .75f, 0, 1);
StartCoroutine(AnswerWait());
}
}
IEnumerator AnswerWait()
{
yield return new WaitForFixedUpdate();
Answer(true);
yield break;
}
public void SetInfoNoImage(string question, int answerid) {
Text txt = GetComponentInChildren<Text>();
txt.text = question;
answersID = answerid;
}
public void SetColor(Color color, bool teacher) {
if (teacher)
{
Image img = GetComponent<Image>();
img.color = new Color(0, 0, 0, 0);
Text txt = GetComponentInChildren<Text>();
txt.color = new Color(0, 0, 0, 1);
Button btn = GetComponent<Button>();
btn.interactable = false;
}
else
{
Image img = GetComponent<Image>();
img.color = color;
}
}
public void SetInfoImage(string question, string idPhoto) {
Text txt = GetComponentInChildren<Text>();
txt.text = question;
Debug.LogWarning(idPhoto);
answersID = System.Convert.ToInt32(idPhoto);
try
{
StartCoroutine(downloadImagee(idPhoto));
}
catch (System.Exception ex)
{
Debug.LogError(ex.Message);
}
}
IEnumerator downloadImagee(string ip)
{
yield return new WaitForSecondsRealtime(1);
yield return StartCoroutine(DownloadImage(ip));
}
IEnumerator DownloadImage(string id) {
string url = "http://" + ReadingFile.iploc + "/images/ans_" + id + ".jpg";
Debug.Log(url);
Image image = GetComponentsInChildren<Image>()[1];
using (WWW www = new WWW(url))
{
yield return www;
image.sprite = Sprite.Create(www.texture, new Rect(0, 0, www.texture.width, www.texture.height),
new Vector2(0, 0));
}
}
public void AnswerBtnVoid()
{
Answer(!ReadingFile.StudentUsers.Contains(ReadingFile.instance.userType));
}
void Answer(bool teacher = false) {
QuestionsController controller = GetComponentInParent<QuestionsController>();
if (!controller.Answered)
{
controller.Answer(isRight, answersID);
if (!teacher)
{
border.SetActive(true);
Image img = border.GetComponent<Image>();
if (isRight)
{
img.color = Color.green;
}
else
{
img.color = Color.red;
}
}
controller.Answered = true;
}
}
public void ReproduzirAudio(bool Correto)
{
this.GetComponent<AudioSource>().clip = Resources.Load<AudioClip>("Questions/" + ((Correto) ? "res_correto" : "res_errado"));
this.GetComponent<AudioSource>().Play();
}
}