您最多选择25个主题
主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
73 行
2.4 KiB
73 行
2.4 KiB
using System;
|
|
|
|
namespace UnityEngine.Perception.Randomization.Parameters
|
|
{
|
|
/// <summary>
|
|
/// Used to apply sampled parameter values to a particular GameObject, Component, and property.
|
|
/// Typically managed by a parameter configuration.
|
|
/// </summary>
|
|
[Serializable]
|
|
class ParameterTarget
|
|
{
|
|
public GameObject gameObject;
|
|
public Component component;
|
|
public string propertyName = "";
|
|
public FieldOrProperty fieldOrProperty;
|
|
public ParameterApplicationFrequency applicationFrequency;
|
|
|
|
public void AssignNewTarget(
|
|
GameObject obj, Component comp, string fieldOrPropertyName, ParameterApplicationFrequency frequency)
|
|
{
|
|
gameObject = obj;
|
|
component = comp;
|
|
propertyName = fieldOrPropertyName;
|
|
applicationFrequency = frequency;
|
|
var componentType = component.GetType();
|
|
fieldOrProperty = componentType.GetField(fieldOrPropertyName) != null
|
|
? FieldOrProperty.Field
|
|
: FieldOrProperty.Property;
|
|
}
|
|
|
|
public void Clear()
|
|
{
|
|
gameObject = null;
|
|
component = null;
|
|
propertyName = string.Empty;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Writes a sampled value to the target GameObject and property
|
|
/// </summary>
|
|
public void ApplyValueToTarget(object value)
|
|
{
|
|
var componentType = component.GetType();
|
|
if (fieldOrProperty == FieldOrProperty.Field)
|
|
{
|
|
var field = componentType.GetField(propertyName);
|
|
if (field == null)
|
|
throw new ParameterValidationException(
|
|
$"Component type {componentType.Name} does not have a field named {propertyName}");
|
|
field.SetValue(component, value);
|
|
}
|
|
else
|
|
{
|
|
var property = componentType.GetProperty(propertyName);
|
|
if (property == null)
|
|
throw new ParameterValidationException(
|
|
$"Component type {componentType.Name} does not have a property named {propertyName}");
|
|
property.SetValue(component, value);
|
|
}
|
|
}
|
|
}
|
|
|
|
enum ParameterApplicationFrequency
|
|
{
|
|
OnIterationSetup,
|
|
EveryFrame
|
|
}
|
|
|
|
enum FieldOrProperty
|
|
{
|
|
Field, Property
|
|
}
|
|
}
|