浏览代码

changed how randomizer state is serialized based on feedback

/0.9.0.preview.1_staging
Mohsen Kamalzadeh 3 年前
当前提交
206b987b
共有 2 个文件被更改,包括 18 次插入53 次删除
  1. 64
      com.unity.perception/Runtime/Randomization/Scenarios/Serialization/ScenarioSerializer.cs
  2. 7
      com.unity.perception/Runtime/Randomization/Scenarios/Serialization/SerializationStructures.cs

64
com.unity.perception/Runtime/Randomization/Scenarios/Serialization/ScenarioSerializer.cs


foreach (var randomizer in randomizers)
{
var randomizerData = SerializeRandomizer(randomizer);
if (randomizerData.items.Count == 0)
if (randomizerData.items.Count == 0 && !randomizerData.state.canBeSwitchedByUser)
continue;
serializedRandomizers.Add(randomizer.GetType().Name, randomizerData);
}

static Group SerializeRandomizer(Randomizer randomizer)
{
var randomizerData = new Group();
randomizerData.state.enabled = randomizer.enabled;
randomizerData.state.canBeSwitchedByUser = randomizer.enabledStateCanBeSwitchedByUser;
var fields = randomizer.GetType().GetFields();
foreach (var field in fields)
{

randomizerData.items.Add(field.Name, new Scalar { value = scalarValue });
}
}
var properties = randomizer.GetType().GetProperties();
foreach (var property in properties)
{
if (property.PropertyType.IsSubclassOf(typeof(Randomization.Parameters.Parameter)))
{
if (!IsSubclassOfRawGeneric(typeof(NumericParameter<>), property.PropertyType))
continue;
var parameter = (Randomization.Parameters.Parameter)property.GetValue(randomizer);
var parameterData = SerializeParameter(parameter);
if (parameterData.items.Count == 0)
continue;
randomizerData.items.Add(property.Name, parameterData);
}
else
{
var scalarValue = ScalarFromProperty(property, randomizer);
if (scalarValue != null)
randomizerData.items.Add(property.Name, new Scalar { value = scalarValue });
}
}
return randomizerData;
}

return null;
}
static IScalarValue ScalarFromProperty(PropertyInfo property, object obj)
{
if (property.PropertyType == typeof(string))
return new StringScalarValue { str = (string)property.GetValue(obj) };
if (property.PropertyType == typeof(bool))
return new BooleanScalarValue { boolean = (bool)property.GetValue(obj) };
if (property.PropertyType == typeof(float) || property.PropertyType == typeof(double) || property.PropertyType == typeof(int))
return new DoubleScalarValue { num = Convert.ToDouble(property.GetValue(obj)) };
//Properties cannot have a Range attribute like fields so no need to check for it here (see function similar to this for fields)
return null;
}
#endregion
#region Deserialization

static void DeserializeRandomizer(Randomizer randomizer, Group randomizerData)
{
randomizer.enabled = randomizerData.state.enabled;
if (field != null)
{
if (pair.Value is Parameter parameterData)
DeserializeParameter((Randomization.Parameters.Parameter)field.GetValue(randomizer), parameterData);
else
DeserializeScalarValue(randomizer, field, (Scalar)pair.Value);
if (field == null)
}
var property = randomizer.GetType().GetProperty(pair.Key);
if (property != null)
{
if (pair.Value is Parameter parameterData)
DeserializeParameter((Randomization.Parameters.Parameter)property.GetValue(randomizer), parameterData);
else
DeserializeScalarValue(randomizer, property, (Scalar)pair.Value);
}
if (pair.Value is Parameter parameterData)
DeserializeParameter((Randomization.Parameters.Parameter)field.GetValue(randomizer), parameterData);
else
DeserializeScalarValue(randomizer, field, (Scalar)pair.Value);
}
}

7
com.unity.perception/Runtime/Randomization/Scenarios/Serialization/SerializationStructures.cs


public string imageLink = string.Empty;
}
class RandomizerStateData
{
public bool enabled;
public bool canBeSwitchedByUser;
}
public RandomizerStateData state = new RandomizerStateData();
[JsonConverter(typeof(GroupItemsConverter))]
public Dictionary<string, IGroupItem> items = new Dictionary<string, IGroupItem>();
}

正在加载...
取消
保存