Created
December 26, 2021 20:42
The Unity script for DentaLab's Exam Assessment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using UnityEngine; | |
using UnityEngine.UI; | |
public class ExamManager : MonoBehaviour | |
{ | |
[Tooltip("The Text for the question display")] | |
public Text question; | |
[Tooltip("The Text for the automatic evaluation display")] | |
public Text evaluation; | |
[Tooltip("The parents of the GameObjects containing each choice")] | |
public GameObject a, b, c, d; | |
[Tooltip("The Text for each choice")] | |
public Text aText, bText, cText, dText; | |
[Tooltip("The navigation Buttons")] | |
public Button back, next; | |
[Tooltip("The Sprites with backgrounds corresponding to selection and choice correctness")] | |
public Sprite defaultImg, selectedImg, wrongImg, rightImg; | |
[Tooltip("The parent GameObjects holding the Next Button and Submit Button respectively")] | |
public GameObject nextObj, submit; | |
bool submitted; // Tracks if the user has submnitted the exam | |
string examFileName = "Exam1"; // The name of the .txt file | |
int index; // Current question index | |
string title; // The exams title | |
// Question data: Question, A-D multiple choice questions, and correct answer | |
List<Tuple<string, string, string, string, string, string>> questions; | |
List<string> answers; // The students answers | |
Button aBtn, bBtn, cBtn, dBtn; // Buttons A, B, C, D | |
private void Start() | |
{ | |
// Gets buttons | |
aBtn = a.GetComponentInChildren<Button>(); | |
bBtn = b.GetComponentInChildren<Button>(); | |
cBtn = c.GetComponentInChildren<Button>(); | |
dBtn = d.GetComponentInChildren<Button>(); | |
// Adds button listeners | |
back.onClick.AddListener(OnBack); | |
next.onClick.AddListener(OnNext); | |
aBtn.onClick.AddListener(OnSelectA); | |
bBtn.onClick.AddListener(OnSelectB); | |
cBtn.onClick.AddListener(OnSelectC); | |
dBtn.onClick.AddListener(OnSelectD); | |
submit.GetComponent<Button>().onClick.AddListener(OnSubmit); | |
LoadExam(examFileName); | |
} | |
void LoadExam(string examFileName) | |
{ | |
/* | |
* Loads given exam from Assets/Assets/Exams directory | |
*/ | |
questions = new List<Tuple<string, string, string, string, string, string>>(); | |
index = 0; | |
string contents = Directory.GetCurrentDirectory() + "/Assets/Assets/Exams/" + examFileName + ".txt"; | |
List<string> lines = File.ReadAllLines(contents).ToList(); | |
title = lines[0]; // Stores title | |
// Stores question, its choices, and answer | |
for (int i = 2; i < lines.Count; i += 8) | |
{ | |
Tuple<string, string, string, string, string, string> question = new Tuple<string, string, string, string, string, string>( | |
lines[i] + " " + lines[i+1], | |
lines[i+2], | |
lines[i+3], | |
lines[i+4], | |
lines[i+5], | |
lines[i+6] | |
); | |
questions.Add(question); | |
} | |
// Fills current answers as blank | |
answers = new List<string>(questions.Count); | |
for (int i = 0; i < questions.Count; i++) | |
answers.Add(""); | |
LoadQuestion(); | |
} | |
void LoadQuestion() | |
{ | |
/* | |
* Loads question onto Canvas | |
*/ | |
// Displays question of current index | |
Tuple<string, string, string, string, string, string> q = questions[index]; | |
question.text = q.Item1; | |
aText.text = q.Item2.Substring(3, q.Item2.Count()-3); | |
bText.text = q.Item3.Substring(3, q.Item3.Count()-3); | |
cText.text = q.Item4.Substring(3, q.Item4.Count()-3); | |
dText.text = q.Item5.Substring(3, q.Item5.Count()-3); | |
ResetButtonColors(); | |
// Sets button to selected color if applicable | |
if (answers[index].Equals("A")) | |
SetAnswerButtonColors(a); | |
else if (answers[index].Equals("B")) | |
SetAnswerButtonColors(b); | |
else if (answers[index].Equals("C")) | |
SetAnswerButtonColors(c); | |
else if (answers[index].Equals("D")) | |
SetAnswerButtonColors(d); | |
// Displays next or submit button | |
if (index == questions.Count - 1) | |
{ | |
nextObj.SetActive(false); | |
submit.SetActive(true); | |
} else | |
{ | |
nextObj.SetActive(true); | |
submit.SetActive(false); | |
} | |
} | |
void ResetButtonColors() | |
{ | |
// Resets button colors back to default | |
a.GetComponentInChildren<Image>().sprite = defaultImg; | |
b.GetComponentInChildren<Image>().sprite = defaultImg; | |
c.GetComponentInChildren<Image>().sprite = defaultImg; | |
d.GetComponentInChildren<Image>().sprite = defaultImg; | |
} | |
void SetAnswerButtonColors(GameObject multi) | |
{ | |
// Sets button color to selected | |
multi.GetComponentInChildren<Image>().sprite = selectedImg; | |
} | |
void OnBack() | |
{ | |
// Goes to previous question | |
if (index > 0) | |
index--; | |
if (submitted) | |
LoadAnswer(); | |
else | |
LoadQuestion(); | |
} | |
void OnNext() | |
{ | |
// Goes to next question | |
if (index < questions.Count - 1) | |
index++; | |
if (submitted) | |
LoadAnswer(); | |
else | |
LoadQuestion(); | |
} | |
void OnSelectA() | |
{ | |
// Stores A as answer to question | |
if (!submitted) | |
{ | |
answers[index] = "A"; | |
ResetButtonColors(); | |
SetAnswerButtonColors(a); | |
} | |
} | |
void OnSelectB() | |
{ | |
// Stores B as answer to question | |
if (!submitted) | |
{ | |
answers[index] = "B"; | |
ResetButtonColors(); | |
SetAnswerButtonColors(b); | |
} | |
} | |
void OnSelectC() | |
{ | |
// Stores C as answer to question | |
if (!submitted) | |
{ | |
answers[index] = "C"; | |
ResetButtonColors(); | |
SetAnswerButtonColors(c); | |
} | |
} | |
void OnSelectD() | |
{ | |
// Stores D as answer to question | |
if (!submitted) | |
{ | |
answers[index] = "D"; | |
ResetButtonColors(); | |
SetAnswerButtonColors(d); | |
} | |
} | |
void SetRightAnswer(GameObject multi) | |
{ | |
// Sets color of button to correct (e.g. green) | |
multi.GetComponentInChildren<Image>().sprite = rightImg; | |
} | |
void SetWrongAnswer(GameObject multi) | |
{ | |
// Sets color of button to incorrect (e.g. red) | |
multi.GetComponentInChildren<Image>().sprite = wrongImg; | |
} | |
void LoadAnswer() | |
{ | |
/* | |
* Loads answer onto Canvas | |
*/ | |
// Displays question and gets correct answer | |
Tuple<string, string, string, string, string, string> q = questions[index]; | |
question.text = q.Item1; | |
aText.text = q.Item2; | |
bText.text = q.Item3; | |
cText.text = q.Item4; | |
dText.text = q.Item5; | |
string correctChoice = q.Item6; | |
ResetButtonColors(); | |
if (answers[index] == correctChoice) // Correct Answer Chosen | |
{ | |
if (answers[index].Equals("A")) | |
SetRightAnswer(a); | |
else if (answers[index].Equals("B")) | |
SetRightAnswer(b); | |
else if (answers[index].Equals("C")) | |
SetRightAnswer(c); | |
else if (answers[index].Equals("D")) | |
SetRightAnswer(d); | |
} else // Incorrect Answer Chosen | |
{ | |
// Display their answer | |
if (answers[index].Equals("A")) | |
SetWrongAnswer(a); | |
else if (answers[index].Equals("B")) | |
SetWrongAnswer(b); | |
else if (answers[index].Equals("C")) | |
SetWrongAnswer(c); | |
else if (answers[index].Equals("D")) | |
SetWrongAnswer(d); | |
// Display correct answer | |
if (correctChoice.Equals("A")) | |
SetRightAnswer(a); | |
else if (correctChoice.Equals("B")) | |
SetRightAnswer(b); | |
else if (correctChoice.Equals("C")) | |
SetRightAnswer(c); | |
else if (correctChoice.Equals("D")) | |
SetRightAnswer(d); | |
} | |
} | |
void OnSubmit() | |
{ | |
submitted = true; | |
// Next button is always available (not submit) | |
nextObj.SetActive(true); | |
submit.SetActive(false); | |
// Counts correctly answered questions | |
int correct = 0; | |
for (int i = 0; i < questions.Count; i++) | |
{ | |
if (answers[i].Equals(questions[i].Item6)) | |
correct++; | |
} | |
// Gives final evaluation and shows answers | |
evaluation.text = correct.ToString() + "/" + questions.Count.ToString(); | |
index = 0; | |
LoadAnswer(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment