您最多选择25个主题
主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
90 行
3.3 KiB
90 行
3.3 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]
|
|
public class ParameterTarget
|
|
{
|
|
[SerializeField] internal GameObject gameObject;
|
|
[SerializeField] internal Component component;
|
|
[SerializeField] internal string propertyName = string.Empty;
|
|
[SerializeField] internal FieldOrProperty fieldOrProperty = FieldOrProperty.Field;
|
|
[SerializeField] internal ParameterApplicationFrequency applicationFrequency = ParameterApplicationFrequency.OnIterationSetup;
|
|
|
|
/// <summary>
|
|
/// Assigns a new target
|
|
/// </summary>
|
|
/// <param name="targetObject">The target GameObject</param>
|
|
/// <param name="targetComponent">The target component on the target GameObject</param>
|
|
/// <param name="fieldOrPropertyName">The name of the property to apply the parameter to</param>
|
|
/// <param name="frequency">How often to apply the parameter to its target</param>
|
|
public void AssignNewTarget(
|
|
GameObject targetObject,
|
|
Component targetComponent,
|
|
string fieldOrPropertyName,
|
|
ParameterApplicationFrequency frequency)
|
|
{
|
|
gameObject = targetObject;
|
|
component = targetComponent;
|
|
propertyName = fieldOrPropertyName;
|
|
applicationFrequency = frequency;
|
|
var componentType = component.GetType();
|
|
fieldOrProperty = componentType.GetField(fieldOrPropertyName) != null
|
|
? FieldOrProperty.Field
|
|
: FieldOrProperty.Property;
|
|
}
|
|
|
|
internal void Clear()
|
|
{
|
|
gameObject = null;
|
|
component = null;
|
|
propertyName = string.Empty;
|
|
}
|
|
|
|
internal 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);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// How often to apply a new sample to a parameter's target
|
|
/// </summary>
|
|
public enum ParameterApplicationFrequency
|
|
{
|
|
/// <summary>
|
|
/// Applies a parameter once every iteration
|
|
/// </summary>
|
|
OnIterationSetup,
|
|
|
|
/// <summary>
|
|
/// Applies a parameter once every frame
|
|
/// </summary>
|
|
EveryFrame
|
|
}
|
|
|
|
enum FieldOrProperty
|
|
{
|
|
Field, Property
|
|
}
|
|
}
|