diff --git a/unity/Assets/Maroon/reusableScripts/ExperimentParameters/ParameterLoader.cs b/unity/Assets/Maroon/reusableScripts/ExperimentParameters/ParameterLoader.cs index 3c1ab9064..f5cb4b3b2 100644 --- a/unity/Assets/Maroon/reusableScripts/ExperimentParameters/ParameterLoader.cs +++ b/unity/Assets/Maroon/reusableScripts/ExperimentParameters/ParameterLoader.cs @@ -29,6 +29,11 @@ public class ParameterLoader : MonoBehaviour /// public UnityEvent parametersLoaded = new UnityEvent(); + /// + /// Invoked when ExperimentParameters have been loaded, which are custom (e.g. received from Javascript WebGL). + /// + public UnityEvent CustomParametersLoaded = new UnityEvent(); + /// /// Invoked when the JSON files have been initialized. /// @@ -48,6 +53,11 @@ public ExperimentParameters MostRecentParameters private set; } + /// + /// Used to know whether there was already an experiment which potentially used the URL fragment parameters on WebGL already + /// + private static bool firstExperimentDefaultParametersLoaded = false; + #region Singleton private static ParameterLoader _instance; public static ParameterLoader Instance @@ -70,9 +80,10 @@ private void Start() // Listener for external json data (sent e.g. via a Javascript button from a website where Maroon is embedded) WebGlReceiver.Instance.OnIncomingData.AddListener((string jsonData) => { LoadJsonFromString(jsonData); + CustomParametersLoaded?.Invoke(); }); #endif - + if (_automaticiallyDetectJsonFiles) { #if UNITY_WEBGL && !UNITY_EDITOR @@ -142,8 +153,22 @@ public void InitJsonFiles(List jsonFiles) /// /// File to load /// The loaded ExperimentParameters - public ExperimentParameters LoadJsonFromFileIndex(int index) + public ExperimentParameters LoadJsonFromFileIndex(int index, bool firstDefaultParametersLoad = false) { +#if UNITY_WEBGL + /* + * Initial config received from Javascript (originating from the URL Fragment config) will be received before the requested experiment is loaded, + * thus if on WebGL and the WebGlReceiver.Instance.MostRecentData is not null and this it the first experiment, load instead the WebGlReceiver.Instance.MostRecentData instead of the requested default file. + */ + if (firstDefaultParametersLoad && !firstExperimentDefaultParametersLoaded && !string.IsNullOrWhiteSpace(WebGlReceiver.Instance.MostRecentData)) + { + firstExperimentDefaultParametersLoaded = true; + CustomParametersLoaded?.Invoke(); + return LoadJsonFromString(WebGlReceiver.Instance.MostRecentData); + } +#endif + + if (index >= _jsonFile.Count) { Debug.LogError("Index " + index + " is greater or equal the number of files " + _jsonFile.Count); @@ -160,7 +185,7 @@ public ExperimentParameters LoadJsonFromFileIndex(int index) /// /// Name of the file to load /// The loaded ExperimentParameters - public ExperimentParameters LoadJsonFromFileName(string name) + public ExperimentParameters LoadJsonFromFileName(string name, bool firstDefaultParametersLoad = false) { int index = IndexOfJson(name); if (index == -1) @@ -169,7 +194,7 @@ public ExperimentParameters LoadJsonFromFileName(string name) return null; } - return LoadJsonFromFileIndex(index); + return LoadJsonFromFileIndex(index, firstDefaultParametersLoad); } /// @@ -179,7 +204,16 @@ public ExperimentParameters LoadJsonFromFileName(string name) /// The loaded ExperimentParameters public ExperimentParameters LoadJsonFromString(string data) { + Debug.Log("Trying to load ExperimentParameters from JSON String."); MostRecentParameters = ConvertJsonToExperimentParameters(data); + if (MostRecentParameters == null) + { + Debug.LogError("Loaded ExperimentParameters are null."); + } + else + { + Debug.Log("Successfully parsed ExperimentParameters: " + MostRecentParameters.GetType()); + } parametersLoaded?.Invoke(MostRecentParameters); return MostRecentParameters; } diff --git a/unity/Assets/Maroon/scenes/experiments/3DMotionSimulation/Scripts/ParameterUI.cs b/unity/Assets/Maroon/scenes/experiments/3DMotionSimulation/Scripts/ParameterUI.cs index 8adf9a462..cd2e0dce9 100644 --- a/unity/Assets/Maroon/scenes/experiments/3DMotionSimulation/Scripts/ParameterUI.cs +++ b/unity/Assets/Maroon/scenes/experiments/3DMotionSimulation/Scripts/ParameterUI.cs @@ -126,22 +126,22 @@ public void OnFilesLoadedInital() #if UNITY_WEBGL && !UNITY_EDITOR if (BootstrappingManager.Instance.UrlParameters.TryGetValue(WebGlUrlParameter.Config, out string config)) { - if (!ApplyConfig(config)) ApplyConfig("Default"); + if (!ApplyConfig(config)) ApplyConfig("Default", true); } else #endif { - ApplyConfig("Default"); + ApplyConfig("Default", true); } ParameterLoader.Instance.OnFilesInitialized.RemoveListener(OnFilesLoadedInital); } - public bool ApplyConfig(string configName) + public bool ApplyConfig(string configName, bool firstDefaultParametersLoad = false) { _currentConfigIndex = ParameterLoader.Instance.IndexOfJson(configName); dropdown.SetValueWithoutNotify(_currentConfigIndex); - var parameters = ParameterLoader.Instance.LoadJsonFromFileName(configName); + var parameters = ParameterLoader.Instance.LoadJsonFromFileName(configName, firstDefaultParametersLoad); return parameters != null; }