您最多选择25个主题
主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
38 行
1.5 KiB
38 行
1.5 KiB
using System.IO;
|
|
|
|
namespace UnityEngine.Perception.Randomization.Scenarios
|
|
{
|
|
/// <summary>
|
|
/// The base class of scenarios with serializable constants
|
|
/// </summary>
|
|
/// <typeparam name="T">The type of constants to serialize</typeparam>
|
|
public abstract class Scenario<T> : ScenarioBase where T : new()
|
|
{
|
|
/// <summary>
|
|
/// A construct containing serializable constants that control the execution of this scenario
|
|
/// </summary>
|
|
public T constants = new T();
|
|
|
|
/// <summary>
|
|
/// Serializes this scenario's constants to a json file in the Unity StreamingAssets folder
|
|
/// </summary>
|
|
public sealed override void Serialize()
|
|
{
|
|
Directory.CreateDirectory(Application.dataPath + "/StreamingAssets/");
|
|
using (var writer = new StreamWriter(serializedConstantsFilePath, false))
|
|
writer.Write(JsonUtility.ToJson(constants, true));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deserializes this scenario's constants from a json file in the Unity StreamingAssets folder
|
|
/// </summary>
|
|
/// <exception cref="ScenarioException"></exception>
|
|
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<T>(jsonText);
|
|
}
|
|
}
|
|
}
|