Made trivia questions work

This commit is contained in:
Pasha Bibko
2026-01-15 11:42:10 +00:00
parent fd61dcb2f4
commit 4eea7f8836
9 changed files with 271 additions and 8 deletions

View File

@@ -1,23 +1,42 @@
using System.IO;
using UnityEngine;
using UnityEngine.UI;
namespace InterfaceOff
{
[System.Serializable] public struct TriviaQuestion
{
/* Disables name warnings because of JSON serialization */
// ReSharper disable once InconsistentNaming
public string question;
// ReSharper disable once InconsistentNaming
public string[] choices;
// ReSharper disable once InconsistentNaming
public string answer;
}
[System.Serializable] public struct TriviaSet
{
/* Disables name warnings because of JSON serialization */
// ReSharper disable once InconsistentNaming
public TriviaQuestion[] questions;
}
public class TriviaWindow : WindowBase
{
private static TriviaSet Trivia;
private static Vector2[] Positions { get; } =
{
new(-110, 30f),
new(110f, 30f),
new(-110, -60),
new(110f, -60)
};
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
private static void LoadTriviaSet()
@@ -30,10 +49,36 @@ namespace InterfaceOff
public override void OnWindowInstantiation()
{
/* Fetches a random question and sets it as the title */
TriviaQuestion question = Trivia.questions[Random.Range(0, Trivia.questions.Length)];
Components.InfoText.text = question.question;
Components.SetWidth(500); // Larger width is needed for this component that others
Components.SetWidth(500);
/* Adds the answer boxes */
for (int i = 0; i < 4; i++)
{
/* Creates the gameobject and fetches the needed components */
GameObject go = Instantiate(PrefabRegistry.Instance.TextButtonPrefab, transform);
Text text = go.GetComponentInChildren<Text>();
Button button = go.GetComponent<Button>();
/* Assigns needed info to the components */
text.text = question.choices[i];
RectTransform buttonTransform = button.GetComponent<RectTransform>();
buttonTransform.anchoredPosition = Positions[i];
buttonTransform.sizeDelta = new Vector2(200, 60);
/* Adds a lambda function to the button press to detect answers */
bool isCorrectButton = string.Equals(question.choices[i], question.answer);
button.onClick.AddListener(() =>
{
if (isCorrectButton)
{
DestroyWindow();
}
});
}
}
}
}