Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Bibliotheken
using UnityEngine; //f. spielentwicklung und UI Elemente
using UnityEngine.UI;
using TMPro; // TMPro (TextMeshPro): erweiterte Textdarstellung verwendet
using System.Collections.Generic; // Verw. generischer Datenstrukturen wie Listen notwendig
public class QuizManager : MonoBehaviour //QuizManager ist Klasse & erbt von MonoBehaviour
{
public TextMeshProUGUI questionText; // TMPU Variabale dient zur anezige der aktuellen frage
public Button[] answerButtons; //array von "Button" wird f. AW Mgl. verwendet
private List<Question> questions = new List<Question>(); // Liste, speichert Objekte vom Typ "Question"
private Question currentQuestion; // verwalten die aktuell gestellten Frage
private int currentQuestionIndex = 0;
// Farben fr AWs: Farbvariablen, werden zur anzeige von feedback verwendet
public Color correctColor = Color.green;
public Color wrongColor = Color.red;
public Color normalColor = Color.white;
// Methoden
void Start() // beim Starten des Skripts wird die Methode aufgerufen: Fragen laden + erste Frage wird gestellt
{
LoadQuestions();
SetCurrentQuestion();
}
void LoadQuestions()
{ // // Quizfragen werden initialisiert + zur "question" Liste hinzugefgt
questions.Add(new Question("Frage 1: Was ist Phishing?", new string[] { "A) Eine Methode zum Fischen", "B)", "C)" }, 1));
}
// Methode aktualisiert die UI mit der aktuellen Frage + den dazugehrigen AWs
// es werden ebenfalls alte eventlistener von dne buttons entfernt und neue hinzugefgt die "CheckAnswer" aufrufen
void SetCurrentQuestion()
{
currentQuestion = questions[currentQuestionIndex];
questionText.text = currentQuestion.text;
for (int i = 0; i < answerButtons.Length; i++)
{
answerButtons[i].GetComponentInChildren<TextMeshProUGUI>().text = currentQuestion.answers[i];
answerButtons[i].GetComponent<Image>().color = normalColor;
answerButtons[i].onClick.RemoveAllListeners();
int capture = i;
answerButtons[i].onClick.AddListener(() => CheckAnswer(capture));
}
}
// berprft ob die gewhlte AW korrekt ist und ndert die Farbe des buttons entsprechend
public void CheckAnswer(int index)
{
if (index == currentQuestion.correctAnswer)
{
answerButtons[index].GetComponent<Image>().color = correctColor;
}
else
{
answerButtons[index].GetComponent<Image>().color = wrongColor;
answerButtons[currentQuestion.correctAnswer].GetComponent<Image>().color = correctColor; //richtige Antwort zeigen
}
}
// wichtig fr das einrichten der nchsten fragen, aktuell aber leer
void SetNextQuestion() { }
}
// Question Klasse enthlt Frage, Fragetext, AW-Mgl. und den Index der richtigen AW
[System.Serializable]
public class Question
{
public string text;
public string[] answers;
public int correctAnswer;
public Question(string text, string[] answers, int correctAnswer)
{
this.text = text;
this.answers = answers;
this.correctAnswer = correctAnswer;
}
}