using System; using UnityEngine; using UnityEngine.Perception.Randomization.Parameters.Attributes; using UnityEngine.Perception.Randomization.Samplers; namespace UnityEngine.Perception.Randomization.Parameters { /// /// Parameters, in conjunction with a parameter configuration, are used to create convenient interfaces for /// randomizing simulations. /// [Serializable] public abstract class Parameter { public string name = "Parameter"; [SerializeField] internal bool collapsed; [HideInInspector] public ParameterTarget target = new ParameterTarget(); public bool hasTarget => target.gameObject != null; /// /// Returns meta information regarding this type of parameter /// public ParameterMetaData MetaData => (ParameterMetaData)Attribute.GetCustomAttribute(GetType(), typeof(ParameterMetaData)); /// /// An array containing a reference to each sampler field in this parameter /// public abstract ISampler[] samplers { get; } /// /// Resets sampler states and then offsets those states using the current scenario iteration /// /// The current scenario iteration public void ResetState(int scenarioIteration) { foreach (var sampler in samplers) sampler.ResetState(scenarioIteration); } /// /// The sample type generated by this parameter /// public abstract Type OutputType { get; } /// /// Applies one sampled value to this parameters assigned target gameobject /// public abstract void ApplyToTarget(int seedOffset); /// /// Validates parameter settings /// public virtual void Validate() { if (hasTarget) { if (target.component == null) throw new ParameterException($"Null component target on parameter \"{name}\""); if (string.IsNullOrEmpty(target.propertyName)) throw new ParameterException($"Invalid property target on parameter \"{name}\""); } } } }