2025-12-11 13:14:43 +01:00

73 lines
2.2 KiB
C#

// QuestionManager.cs
// Zweck: Lädt ein gültiges JSON aus Resources (beliebiger Name), verwaltet aktuellen Fragenindex.
using UnityEngine;
public class QuestionManager : MonoBehaviour
{
public QuestionList questionList; // geladene Fragenliste
private int currentQuestionIndex = 0; // aktueller Index
public static QuestionManager Instance { get; private set; }
private void Awake()
{
// Singleton + erhalten über Szenen
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
return;
}
LoadQuestions();
}
private void LoadQuestions()
{
// Alle TextAssets aus Resources durchgehen (keine feste Dateibenennung)
TextAsset[] jsonFiles = Resources.LoadAll<TextAsset>("");
bool foundValidJson = false;
foreach (var file in jsonFiles)
{
// einfache Plausibilitätsprüfung
if (file.text.TrimStart().StartsWith("{"))
{
try
{
questionList = JsonUtility.FromJson<QuestionList>(file.text);
Debug.Log($"JSON geladen: {file.name} mit {questionList.questions.Length} Fragen.");
foundValidJson = true;
return; // erstes gültiges JSON nehmen
}
catch (System.Exception ex)
{
Debug.LogError($"JSON-Parsing fehlgeschlagen ({file.name}): {ex.Message}");
}
}
}
if (!foundValidJson)
Debug.LogError("Kein gültiges JSON im Resources-Ordner gefunden!");
}
public Question GetCurrentQuestion()
{
if (questionList == null || questionList.questions == null) return null;
if (currentQuestionIndex < questionList.questions.Length)
return questionList.questions[currentQuestionIndex];
return null;
}
public int GetCurrentIndex() => currentQuestionIndex;
public void AdvanceToNextQuestion() => currentQuestionIndex++;
public void ResetToFirstQuestion() => currentQuestionIndex = 0;
}