using System.IO; namespace UnityEngine.Experimental.Perception.Randomization.Scenarios { /// /// The base class of scenarios with serializable constants /// /// The type of constants to serialize public abstract class Scenario : ScenarioBase where T : new() { /// /// A construct containing serializable constants that control the execution of this scenario /// public T constants = new T(); /// /// Serializes this scenario's constants to a json file in the Unity StreamingAssets folder /// public sealed override void Serialize() { Directory.CreateDirectory(Application.dataPath + "/StreamingAssets/"); using (var writer = new StreamWriter(serializedConstantsFilePath, false)) writer.Write(JsonUtility.ToJson(constants, true)); } /// /// Deserializes this scenario's constants from a json file in the Unity StreamingAssets folder /// /// public sealed override void Deserialize() { if (!File.Exists(serializedConstantsFilePath)) throw new ScenarioException($"JSON scenario constants file does not exist at path {serializedConstantsFilePath}"); var jsonText = File.ReadAllText(serializedConstantsFilePath); constants = JsonUtility.FromJson(jsonText); } } }