Skip to content

Instantly share code, notes, and snippets.

@unity3dcollege
Last active July 12, 2017 20:04
Show Gist options
  • Save unity3dcollege/51fdc08cf8125fe9e4f0ea6c7f1f3eda to your computer and use it in GitHub Desktop.
Save unity3dcollege/51fdc08cf8125fe9e4f0ea6c7f1f3eda to your computer and use it in GitHub Desktop.
using System;
using UnityEngine;
using UnityEngine.UI;
public class UIController : MonoBehaviour
{
[SerializeField]
private Text questionText;
[SerializeField]
private Button[] answerButtons;
[SerializeField]
private GameObject correctAnswerPopup;
[SerializeField]
private GameObject wrongAnswerPopup;
public void SetupUIForQuestion(QuizQuestion question)
{
correctAnswerPopup.SetActive(false);
wrongAnswerPopup.SetActive(false);
questionText.text = question.Question;
for (int i = 0; i < question.Answers.Length; i++)
{
answerButtons[i].GetComponentInChildren<Text>().text = question.Answers[i];
answerButtons[i].gameObject.SetActive(true);
}
for (int i = question.Answers.Length; i < answerButtons.Length; i++)
{
answerButtons[i].gameObject.SetActive(false);
}
}
public void HandleSubmittedAnswer(bool isCorrect)
{
ToggleAnswerButtons(false);
if (isCorrect)
{
ShowCorrectAnswerPopup();
}
else
{
ShowWrongAnswerPopup();
}
}
private void ToggleAnswerButtons(bool value)
{
for (int i = 0; i < answerButtons.Length; i++)
{
answerButtons[i].gameObject.SetActive(value);
}
}
private void ShowCorrectAnswerPopup()
{
correctAnswerPopup.SetActive(true);
}
private void ShowWrongAnswerPopup()
{
wrongAnswerPopup.SetActive(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment