Documentation for the Unity C# Library
Loading...
Searching...
No Matches
AnswerOption.cs
Go to the documentation of this file.
1using System;
2using TMPro;
3using UnityEngine;
4using UnityEngine.UI;
5
7{
8 public class AnswerOption : MonoBehaviour
9 {
10 [SerializeField] private TMP_Text answerOption;
11 [SerializeField] private Toggle toggle;
12 [SerializeField] private bool isAnswerCorrect;
13 public int toggleID;
14
15 public event Action<string, bool, int> ToggleSelected;
16
17 private bool _isSelected;
18
19 private void Start()
20 {
21 toggle = GetComponent<Toggle>();
22 }
23
24 private void OnEnable()
25 {
26 toggle.onValueChanged.AddListener(OnToggleSelected);
27 }
29 private void OnDisable()
30 {
31 toggle.onValueChanged.RemoveListener(OnToggleSelected);
32 }
34 private void OnToggleSelected(bool isSelected)
35 {
36 if (!_isSelected)
37 {
38 _isSelected = isSelected;
39 SetTextColor(isSelected);
41 }
42 else
43 {
44 toggle.SetIsOnWithoutNotify(true);
45 }
46 }
47
48 public void SetSelected(bool isSelected)
49 {
50 _isSelected = isSelected;
51 toggle.SetIsOnWithoutNotify(isSelected);
52 SetTextColor(isSelected);
53 }
54
55 public void SetText(string option)
56 {
57 answerOption.text = option;
58 }
59
60 public void SetCorrectnes(bool isCorrect)
61 {
62 isAnswerCorrect = isCorrect;
63 }
65 private void SetTextColor(bool isSelected)
66 {
67 answerOption.color = isSelected ? Color.white : Color.black;
68 }
69 }
70}
void SetCorrectnes(bool isCorrect)
TMP_Text answerOption
[SerializeField]
void SetText(string option)
void SetSelected(bool isSelected)
Toggle toggle
[SerializeField]
void SetTextColor(bool isSelected)
bool isAnswerCorrect
[SerializeField]
void OnToggleSelected(bool isSelected)
Action< string, bool, int > ToggleSelected