diff --git a/Packages/vr.teachr.measurements/Runtime/ScenarioSequence/Scripts/ScenarioController.cs b/Packages/vr.teachr.measurements/Runtime/ScenarioSequence/Scripts/ScenarioController.cs index 7b1e97a30911539edc65d29afb130639690717ce..280a7c8cc13c525b155a5756dca9158e523a18f5 100644 --- a/Packages/vr.teachr.measurements/Runtime/ScenarioSequence/Scripts/ScenarioController.cs +++ b/Packages/vr.teachr.measurements/Runtime/ScenarioSequence/Scripts/ScenarioController.cs @@ -37,17 +37,24 @@ namespace TeachR.Measurements /// <summary> /// Gets the singleton instance of the ScenarioController. /// </summary> - public static ScenarioController Instance { get; private set; } + public static ScenarioController Instance + { + get + { + if (_instance == null) + _instance = FindFirstObjectByType<ScenarioController>(); + return _instance; + } + } private void Awake() { // Singleton - if (_instance != null && _instance != this) - Destroy(this); - else + if (_instance == null) _instance = this; + else if (_instance != this) + Destroy(gameObject); - scenarioSequences = new(); scenarioFolder = Path.Combine(Application.streamingAssetsPath, "Config~", "sequence"); FeatureRegister.RegisterFeature(this, true); InitGlobals.WebsocketInitHook += WebInit; @@ -57,6 +64,7 @@ namespace TeachR.Measurements void Start() { enviroment = gameObject; + LoadScenarios(); SetHeader("timestamp;target;activity\n" + $"{DateTime.Now.ToString(DateTimeFormat)};-;-"); } @@ -91,10 +99,8 @@ namespace TeachR.Measurements { CoachRtc.Transient.On("scenarioChange", json => { - Debug.Log($"[SCENARIO] received scenario change: {json.content}"); JObject jsonObj = JsonConvert.DeserializeObject<JObject>(json.content); string scenarioName = jsonObj["scenario"]?.ToString(); - Debug.Log($"scenario name: {scenarioName}"); StartScenarioByName(scenarioName); }); CoachRtc.Transient.On("requestScenarios", _ => @@ -106,70 +112,48 @@ namespace TeachR.Measurements } /// <summary> - /// Retrieves the names of all scenario files stored in the streaming assets folder. + /// Preload all scenarios from asset folder and store them in <see cref="scenarioSequences"/>. + /// </summary> + public void LoadScenarios() + { + scenarioSequences = new List<ScenarioSequence>(); + foreach (string filepath in GetScenarioPaths()) + { + var path = Path.GetDirectoryName(filepath); + var filename = Path.GetFileName(filepath); + scenarioSequences.Add(new ScenarioSequence(filename, path)); + } + } + + /// <summary> + /// Retrieves the names of all scenarios in <see cref="scenarioSequences"/>. /// </summary> - /// <returns>A list of strings representing the names of scenario files.</returns> + /// <returns>A list of strings representing the names of scenarios.</returns> public List<string> CollectScenarioNames() { List<string> fileNames = new(); - string path = Path.Combine(Application.streamingAssetsPath, scenarioFolder); - if (Directory.Exists(path)) + foreach (ScenarioSequence seqence in scenarioSequences) { - var dirs = Directory.GetDirectories(path); - foreach (string dir in dirs) - { - var files = Directory.GetFiles(dir, "*.xml"); - foreach (string filename in files) - fileNames.Add(Path.GetFileNameWithoutExtension(filename)); - } + fileNames.Add(Path.GetFileNameWithoutExtension(seqence.Name)); } return fileNames; } private void StartScenarioByName(string scenarioName) { - Debug.Log($"[SCENARIO] Trying to start scenario: {scenarioName}"); - - if (scenarioSequences == null) + if (scenarioSequences == null || scenarioSequences.Count == 0) { - Debug.LogError("[SCENARIO] scenarioSequences is NULL. Initializing..."); - scenarioSequences = new List<ScenarioSequence>(); - } - - if (scenarioSequences.Count == 0) - { - Debug.LogWarning("[SCENARIO] scenarioSequences is empty, loading from files..."); - string path = Path.Combine(Application.streamingAssetsPath, "Config~", "sequence"); - var scenarioPaths = GetScenarioPaths(); - - foreach (string filePath in scenarioPaths) - { - string fileName = Path.GetFileName(filePath); - string dirPath = Path.GetDirectoryName(filePath); - Debug.Log($"[SCENARIO] Loading scenario from: {filePath}"); - - ScenarioSequence newSequence = new ScenarioSequence(fileName, dirPath); - if (newSequence == null || newSequence.scenarioEvents == null || newSequence.scenarioEvents.Count == 0) - { - Debug.LogError($"[SCENARIO] Failed to load scenario: {fileName}"); - continue; - } - - scenarioSequences.Add(newSequence); - Debug.Log($"[SCENARIO] Successfully added scenario: {newSequence.Name}"); - } + Debug.LogError("scenarioSequences is null or empty."); + return; } - scenarioName = Path.GetFileNameWithoutExtension(scenarioName); - ScenarioSequence sequence = scenarioSequences.FirstOrDefault(seq => seq.Name.Equals(scenarioName, StringComparison.OrdinalIgnoreCase)); - + ScenarioSequence sequence = scenarioSequences.FirstOrDefault(seq => seq.Name.Equals(scenarioName + ".xml", StringComparison.OrdinalIgnoreCase)); if (sequence == null) { - Debug.LogError($"[SCENARIO] Scenario '{scenarioName}' not found."); + Debug.LogError($"Scenario {scenarioName} not found."); return; } - Debug.Log($"[SCENARIO] Starting scenario: {sequence.Name}"); StartScenario(sequence); } @@ -288,19 +272,13 @@ namespace TeachR.Measurements if (EditorGUILayout.DropdownButton(new GUIContent("Select sequence ..."), FocusType.Passive)) { GenericMenu menu = new GenericMenu(); - foreach (string filepath in ScenarioController.GetScenarioPaths()) + foreach (var sequence in ScenarioController.scenarioSequences) { - var path = Path.GetDirectoryName(filepath); - var filename = Path.GetFileName(filepath); - ScenarioSequence sequene = new ScenarioSequence(filename, path); - menu.AddItem(new GUIContent(filename), false, () => + menu.AddItem(new GUIContent(sequence.Name), false, () => { - if (!string.IsNullOrEmpty(filename)) - { - if(ScenarioController.Instance.currentSequence != null) - ScenarioController.Instance.StopAllEvents(); - ScenarioController.Instance.StartScenario(sequene); - } + if (ScenarioController.Instance.currentSequence != null) + ScenarioController.Instance.StopAllEvents(); + ScenarioController.Instance.StartScenario(sequence); }); } menu.ShowAsContext(); diff --git a/Packages/vr.teachr.measurements/Runtime/Scripts/MeasureController.cs b/Packages/vr.teachr.measurements/Runtime/Scripts/MeasureController.cs index 761b20798c8f7c8a8709576f7f15a3bbffb49881..c043b1148354e007631e360b90d028ead389003e 100644 --- a/Packages/vr.teachr.measurements/Runtime/Scripts/MeasureController.cs +++ b/Packages/vr.teachr.measurements/Runtime/Scripts/MeasureController.cs @@ -8,7 +8,6 @@ namespace TeachR.Measurements { public static event Action SavingHook; - // Start is called before the first frame update //void Awake() //{