티스토리 뷰
Button x 4
Button Text x 4
QuizManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class QuizManager : MonoBehaviour
{
public List<QuestionAndAnswers> QnA;
public GameObject[] optioins;
public int currentQuestion;
public GameObject Quizpanel;
public GameObject GoPanel;
public Text QuestionText;
public Text ScoreText;
int totalQuestions = 0;
public int score;
private void Start()
{
totalQuestions = QnA.Count;
GoPanel.SetActive(false);
generateQuestion();
}
public void retry()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
void GameOver()
{
Quizpanel.SetActive(false);
GoPanel.SetActive(true);
ScoreText.text = score + "/" + totalQuestions;
}
public void correct()
{
score += 1;
QnA.RemoveAt(currentQuestion);
generateQuestion();
}
public void wrong()
{
QnA.RemoveAt(currentQuestion);
generateQuestion();
}
void SetAnswers()
{
for(int i =0;i< optioins.Length;i++)
{
optioins[i].GetComponent<AnswerScript>().isCorrect = false;
optioins[i].transform.GetChild(0).GetComponent<Text>().text = QnA[currentQuestion].Answers[i];
if(QnA[currentQuestion].CorrectAnswer == i + 1)
{
optioins[i].GetComponent<AnswerScript>().isCorrect = true;
}
}
}
void generateQuestion()
{
if(QnA.Count > 0)
{
currentQuestion = Random.Range(0, QnA.Count);
QuestionText.text = QnA[currentQuestion].Question;
SetAnswers();
}
else
{
Debug.Log("Out of Questions");
GameOver();
}
}
}
'프로그래밍 > 유니티' 카테고리의 다른 글
유니티 플래피버드 게임 만들기 #2 (0) | 2022.05.17 |
---|---|
유니티 플래피버드 게임 만들기 #1 (0) | 2022.05.15 |
유니티 사운드 여러개 출력하는 방법 (0) | 2022.01.05 |
유니티에서 SQLite 안드로이드에서 실행해보기 (0) | 2021.12.29 |
유니티 안드로이드 데이터베이스 SQLite (0) | 2021.12.06 |
댓글