您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

113 行
3.6 KiB

using System;
using UnityEngine;
using UnityEngine.Experimental.Perception.Randomization.Samplers;
namespace UnityEngine.Experimental.Perception.Randomization.Parameters
{
/// <summary>
/// Parameters, in conjunction with a parameter configuration, are used to create convenient interfaces for
/// randomizing simulations.
/// </summary>
[Serializable]
public abstract class Parameter
{
/// <summary>
/// Returns the display name of a parameter type
/// </summary>
/// <param name="type">A subclass of Parameter</param>
/// <returns>The parameter type's display name</returns>
public static string GetDisplayName(Type type)
{
return type.Name.Replace("Parameter", "");
}
[HideInInspector, SerializeField] internal bool collapsed;
/// <summary>
/// The name of the parameter
/// </summary>
[HideInInspector] public string name = "Parameter";
/// <summary>
/// The target this parameter apply a sample to
/// </summary>
[HideInInspector, SerializeField] public ParameterTarget target = new ParameterTarget();
/// <summary>
/// Indicates whether this parameter has a target GameObject
/// </summary>
public bool hasTarget => target.gameObject != null;
/// <summary>
/// The sample type generated by this parameter
/// </summary>
public abstract Type sampleType { get; }
/// <summary>
/// An array containing a reference to each sampler field in this parameter
/// </summary>
public abstract ISampler[] samplers { get; }
/// <summary>
/// Constructs a new parameter
/// </summary>
protected Parameter()
{
InitializeSamplers();
}
/// <summary>
/// Deterministically ensures that no sampler shares the same seed when a parameter is initialized
/// </summary>
void InitializeSamplers()
{
var i = 0;
foreach (var sampler in samplers)
{
sampler.IterateState(i++);
sampler.ResetState();
}
}
internal void RandomizeSamplers()
{
foreach (var sampler in samplers)
{
sampler.baseSeed = SamplerUtility.GenerateRandomSeed();
sampler.ResetState();
}
}
/// <summary>
/// Resets sampler states and then offsets those states using the current scenario iteration
/// </summary>
/// <param name="scenarioIteration">The current scenario iteration</param>
public void ResetState(int scenarioIteration)
{
foreach (var sampler in samplers)
{
sampler.ResetState();
sampler.IterateState(scenarioIteration);
}
}
/// <summary>
/// Applies one sampled value to this parameters assigned target gameobject
/// </summary>
internal abstract void ApplyToTarget(int seedOffset);
/// <summary>
/// Validates parameter settings
/// </summary>
internal virtual void Validate()
{
if (hasTarget)
{
if (target.component == null)
throw new ParameterValidationException($"Null component target on parameter \"{name}\"");
if (string.IsNullOrEmpty(target.propertyName))
throw new ParameterValidationException($"Invalid property target on parameter \"{name}\"");
}
}
}
}