浏览代码

ParameterConfigurations now derive from ParameterBehaviour

/parameter-bahaviours
Steven Leal 4 年前
当前提交
b9a6af2c
共有 19 个文件被更改,包括 155 次插入24 次删除
  1. 12
      com.unity.perception/Editor/Randomization/Editors/ParameterConfigurationEditor.cs
  2. 4
      com.unity.perception/Editor/Randomization/VisualElements/ConfiguredParameterElement.cs
  3. 4
      com.unity.perception/Runtime/Randomization/ParameterBehaviours/ParameterBehaviour.cs
  4. 8
      com.unity.perception/Tests/Runtime/Randomization/ParameterConfigurationTests.cs
  5. 4
      com.unity.perception/Tests/Runtime/Randomization/ScenarioTests.cs
  6. 2
      com.unity.perception/Runtime/Randomization/ParameterBehaviours/Configuration/ParameterConfigurationException.cs
  7. 6
      com.unity.perception/Runtime/Randomization/ParameterBehaviours/Configuration/ParameterTarget.cs
  8. 3
      com.unity.perception/Runtime/Randomization/ParameterBehaviours/Configuration.meta
  9. 28
      com.unity.perception/Runtime/Randomization/ParameterBehaviours/Configuration/ConfiguredParameter.cs
  10. 105
      com.unity.perception/Runtime/Randomization/ParameterBehaviours/Configuration/ParameterConfiguration.cs
  11. 3
      com.unity.perception/Runtime/Randomization/Configuration.meta
  12. 0
      /com.unity.perception/Runtime/Randomization/ParameterBehaviours/Configuration/ParameterConfiguration.cs.meta
  13. 0
      /com.unity.perception/Runtime/Randomization/ParameterBehaviours/Configuration/ParameterConfigurationException.cs
  14. 0
      /com.unity.perception/Runtime/Randomization/ParameterBehaviours/Configuration/ParameterConfigurationException.cs.meta
  15. 0
      /com.unity.perception/Runtime/Randomization/ParameterBehaviours/Configuration/ConfiguredParameter.cs.meta
  16. 0
      /com.unity.perception/Runtime/Randomization/ParameterBehaviours/Configuration/ParameterTarget.cs.meta
  17. 0
      /com.unity.perception/Runtime/Randomization/ParameterBehaviours/Configuration/ParameterTarget.cs

12
com.unity.perception/Editor/Randomization/Editors/ParameterConfigurationEditor.cs


using UnityEngine;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine.Perception.Randomization.Configuration;
using UnityEngine.Perception.Randomization.ParameterBehaviours.Configuration;
using UnityEngine.Perception.Randomization.Parameters;
using UnityEngine.UIElements;

foreach (var child in m_ParameterContainer.Children())
{
var paramIndex = m_ParameterContainer.IndexOf(child);
var param = config.parameters[paramIndex];
var param = config.configuredParameters[paramIndex];
((ConfiguredParameterElement)child).filtered = param.name.ToLower().Contains(lowerFilter);
}
}

{
var paramIndex = m_ParameterContainer.IndexOf(template);
m_ParameterContainer.RemoveAt(paramIndex);
config.parameters.RemoveAt(paramIndex);
config.configuredParameters.RemoveAt(paramIndex);
serializedObject.Update();
RefreshParameterElements();
}

nextIndex--;
var parameterElement = m_ParameterContainer[currentIndex];
var parameter = config.parameters[currentIndex];
var parameter = config.configuredParameters[currentIndex];
config.parameters.RemoveAt(currentIndex);
config.configuredParameters.RemoveAt(currentIndex);
config.parameters.Insert(nextIndex, parameter);
config.configuredParameters.Insert(nextIndex, parameter);
serializedObject.Update();

4
com.unity.perception/Editor/Randomization/VisualElements/ConfiguredParameterElement.cs


using System.Collections.Generic;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine.Perception.Randomization.Configuration;
using UnityEngine.Perception.Randomization.ParameterBehaviours.Configuration;
using UnityEngine.Perception.Randomization.Parameters;
using UnityEngine.UIElements;

ToggleTargetContainer();
var frequencyField = this.Q<EnumField>("application-frequency");
frequencyField.Init(ParameterApplicationFrequency.OnIterationSetup);
frequencyField.Init(ParameterApplicationFrequency.OnIterationStart);
var applicationFrequency = m_Target.FindPropertyRelative("applicationFrequency");
frequencyField.BindProperty(applicationFrequency);

4
com.unity.perception/Runtime/Randomization/ParameterBehaviours/ParameterBehaviour.cs


public abstract IEnumerable<Parameter> parameters { get; }
void Awake()
protected virtual void OnEnable()
void OnDestroy()
protected virtual void OnDisable()
{
activeBehaviours.Remove(this);
}

8
com.unity.perception/Tests/Runtime/Randomization/ParameterConfigurationTests.cs


using System.Collections;
using NUnit.Framework;
using NUnit.Framework;
using UnityEngine.Perception.Randomization.Configuration;
using UnityEngine.Perception.Randomization.ParameterBehaviours.Configuration;
using UnityEngine.TestTools;
namespace RandomizationTests
{

var config = m_TestObject.AddComponent<ParameterConfiguration>();
config.AddParameter<FloatParameter>("SameName");
config.AddParameter<BooleanParameter>("SameName");
Assert.Throws<ParameterConfigurationException>(() => config.ValidateParameters());
Assert.Throws<ParameterConfigurationException>(() => config.Validate());
}
[Test]

4
com.unity.perception/Tests/Runtime/Randomization/ScenarioTests.cs


using NUnit.Framework;
using UnityEngine;
using UnityEngine.Perception.GroundTruth;
using UnityEngine.Perception.Randomization.Configuration;
using UnityEngine.Perception.Randomization.ParameterBehaviours.Configuration;
using UnityEngine.Perception.Randomization.Parameters;
using UnityEngine.Perception.Randomization.Samplers;
using UnityEngine.Perception.Randomization.Scenarios;

var prevPosition = new Vector3();
transform.position = prevPosition;
configuredParameter.target.AssignNewTarget(
m_TestObject, transform, "position", ParameterApplicationFrequency.OnIterationSetup);
m_TestObject, transform, "position", ParameterApplicationFrequency.OnIterationStart);
yield return CreateNewScenario(2, 2);

2
com.unity.perception/Runtime/Randomization/ParameterBehaviours/Configuration/ParameterConfigurationException.cs


using System;
namespace UnityEngine.Perception.Randomization.Configuration
namespace UnityEngine.Perception.Randomization.ParameterBehaviours.Configuration
{
[Serializable]
class ParameterConfigurationException : Exception

6
com.unity.perception/Runtime/Randomization/ParameterBehaviours/Configuration/ParameterTarget.cs


using System;
using UnityEngine.Perception.Randomization.Parameters;
namespace UnityEngine.Perception.Randomization.Configuration
namespace UnityEngine.Perception.Randomization.ParameterBehaviours.Configuration
{
/// <summary>
/// Used to apply sampled parameter values to a particular GameObject, Component, and property.

[SerializeField] internal Component component;
[SerializeField] internal string propertyName = string.Empty;
[SerializeField] internal FieldOrProperty fieldOrProperty = FieldOrProperty.Field;
[SerializeField] internal ParameterApplicationFrequency applicationFrequency = ParameterApplicationFrequency.OnIterationSetup;
[SerializeField] internal ParameterApplicationFrequency applicationFrequency = ParameterApplicationFrequency.OnIterationStart;
/// <summary>
/// Assigns a new target

/// <summary>
/// Applies a parameter once every iteration
/// </summary>
OnIterationSetup,
OnIterationStart,
/// <summary>
/// Applies a parameter once every frame

3
com.unity.perception/Runtime/Randomization/ParameterBehaviours/Configuration.meta


fileFormatVersion: 2
guid: e71c37bf9c4144728de471f853a09a19
timeCreated: 1598290624

28
com.unity.perception/Runtime/Randomization/ParameterBehaviours/Configuration/ConfiguredParameter.cs


using System;
using UnityEngine.Perception.Randomization.Parameters;
namespace UnityEngine.Perception.Randomization.ParameterBehaviours.Configuration
{
[Serializable]
class ConfiguredParameter
{
public string name = "Parameter";
[SerializeReference] public Parameter parameter;
[HideInInspector, SerializeField] public ParameterTarget target = new ParameterTarget();
public bool hasTarget => target.gameObject != null;
public void ApplyToTarget()
{
if (!hasTarget)
return;
target.ApplyValueToTarget(parameter.GenericSample());
}
public void Validate()
{
target.Validate();
parameter.Validate();
}
}
}

105
com.unity.perception/Runtime/Randomization/ParameterBehaviours/Configuration/ParameterConfiguration.cs


using System;
using System.Collections.Generic;
using Randomization.ParameterBehaviours;
using UnityEngine;
using UnityEngine.Perception.Randomization.Parameters;
namespace UnityEngine.Perception.Randomization.ParameterBehaviours.Configuration
{
/// <summary>
/// Creates parameter interfaces for randomizing simulations
/// </summary>
[ExecuteInEditMode]
[AddComponentMenu("Perception/Randomization/ParameterConfiguration")]
public class ParameterConfiguration : ParameterBehaviour
{
[SerializeReference] internal List<ConfiguredParameter> configuredParameters = new List<ConfiguredParameter>();
public override IEnumerable<Parameter> parameters
{
get
{
foreach (var configParameter in configuredParameters)
yield return configParameter.parameter;
}
}
internal ConfiguredParameter AddParameter<T>(string parameterName) where T : Parameter, new()
{
var parameter = new T();
var configParameter = new ConfiguredParameter { name = parameterName, parameter = parameter };
configuredParameters.Add(configParameter);
return configParameter;
}
internal ConfiguredParameter AddParameter(string parameterName, Type parameterType)
{
if (!parameterType.IsSubclassOf(typeof(Parameter)))
throw new ParameterConfigurationException($"Cannot add non-parameter types ({parameterType})");
var parameter = (Parameter)Activator.CreateInstance(parameterType);
var configParameter = new ConfiguredParameter { name = parameterName, parameter = parameter };
configuredParameters.Add(configParameter);
return configParameter;
}
/// <summary>
/// Find a parameter in this configuration by name
/// </summary>
/// <param name="parameterName">The name of the parameter to lookup</param>
/// <param name="parameterType">The type of parameter to lookup</param>
/// <returns>The parameter if found, null otherwise</returns>
/// <exception cref="ParameterConfigurationException"></exception>
public Parameter GetParameter(string parameterName, Type parameterType)
{
foreach (var configParameter in configuredParameters)
{
if (configParameter.name == parameterName && configParameter.parameter.GetType() == parameterType)
return configParameter.parameter;
}
return null;
}
/// <summary>
/// Find a parameter in this configuration by name and type
/// </summary>
/// <param name="parameterName"></param>
/// <typeparam name="T">The type of parameter to look for</typeparam>
/// <returns>The parameter if found, null otherwise</returns>
public T GetParameter<T>(string parameterName) where T : Parameter
{
foreach (var configuredParameter in configuredParameters)
{
if (configuredParameter.name == parameterName && configuredParameter.parameter is T typedParameter)
return typedParameter;
}
return null;
}
public override void OnIterationStart()
{
foreach (var configParameter in configuredParameters)
if (configParameter.target.applicationFrequency == ParameterApplicationFrequency.OnIterationStart)
configParameter.ApplyToTarget();
}
public override void OnFrameStart()
{
foreach (var configParameter in configuredParameters)
if (configParameter.target.applicationFrequency == ParameterApplicationFrequency.EveryFrame)
configParameter.ApplyToTarget();
}
public override void Validate()
{
var parameterNames = new HashSet<string>();
foreach (var configParameter in configuredParameters)
{
if (parameterNames.Contains(configParameter.name))
throw new ParameterConfigurationException(
$"Two or more parameters cannot share the same name (\"{configParameter.name}\")");
parameterNames.Add(configParameter.name);
configParameter.Validate();
}
}
}
}

3
com.unity.perception/Runtime/Randomization/Configuration.meta


fileFormatVersion: 2
guid: b5afd2495bca4fdca95b86d9113f27b2
timeCreated: 1594059945

/com.unity.perception/Runtime/Randomization/Configuration/ParameterConfiguration.cs.meta → /com.unity.perception/Runtime/Randomization/ParameterBehaviours/Configuration/ParameterConfiguration.cs.meta

/com.unity.perception/Runtime/Randomization/Configuration/ParameterConfigurationException.cs → /com.unity.perception/Runtime/Randomization/ParameterBehaviours/Configuration/ParameterConfigurationException.cs

/com.unity.perception/Runtime/Randomization/Configuration/ParameterConfigurationException.cs.meta → /com.unity.perception/Runtime/Randomization/ParameterBehaviours/Configuration/ParameterConfigurationException.cs.meta

/com.unity.perception/Runtime/Randomization/Configuration/ConfiguredParameter.cs.meta → /com.unity.perception/Runtime/Randomization/ParameterBehaviours/Configuration/ConfiguredParameter.cs.meta

/com.unity.perception/Runtime/Randomization/Configuration/ParameterTarget.cs.meta → /com.unity.perception/Runtime/Randomization/ParameterBehaviours/Configuration/ParameterTarget.cs.meta

/com.unity.perception/Runtime/Randomization/Configuration/ParameterTarget.cs → /com.unity.perception/Runtime/Randomization/ParameterBehaviours/Configuration/ParameterTarget.cs

正在加载...
取消
保存