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();
///
/// Returns this scenario's non-typed serialized constants
///
public override object genericConstants => constants;
///
/// Serializes this scenario's constants to a json file in the Unity StreamingAssets folder
///
public 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 override void Deserialize()
{
if (string.IsNullOrEmpty(serializedConstantsFilePath))
{
Debug.Log("No constants file specified. Running scenario with built in constants.");
}
else if (File.Exists(serializedConstantsFilePath))
{
var jsonText = File.ReadAllText(serializedConstantsFilePath);
constants = JsonUtility.FromJson(jsonText);
}
else
{
Debug.LogWarning($"JSON scenario constants file does not exist at path {serializedConstantsFilePath}");
}
}
}
}