浏览代码

working on new workflow

/parameter-bahaviours
Steven Leal 4 年前
当前提交
7d0ec48a
共有 25 个文件被更改,包括 201 次插入605 次删除
  1. 2
      com.unity.perception/Editor/Randomization/Uss/ParameterStyles.uss
  2. 10
      com.unity.perception/Editor/Randomization/Uxml/ParameterListItemElement.uxml
  3. 129
      com.unity.perception/Editor/Randomization/VisualElements/ParameterListItemElement.cs
  4. 34
      com.unity.perception/Runtime/Randomization/ParameterBehaviours/ParameterBehaviour.cs
  5. 20
      com.unity.perception/Runtime/Randomization/Scenarios/ScenarioBase.cs
  6. 73
      com.unity.perception/Editor/Randomization/Editors/ParametersAssetEditor.cs
  7. 46
      com.unity.perception/Editor/Randomization/Uss/ParameterAssetStyles.uss
  8. 21
      com.unity.perception/Editor/Randomization/Uxml/ParameterAssetEditor.uxml
  9. 33
      com.unity.perception/Runtime/Randomization/ParameterBehaviours/ParameterAsset.cs
  10. 30
      com.unity.perception/Editor/Randomization/Editors/ParameterBehaviourEditor.cs
  11. 3
      com.unity.perception/Editor/Randomization/Editors/ParameterBehaviourEditor.cs.meta
  12. 11
      com.unity.perception/Editor/Randomization/Editors/ParameterListEditor.cs.meta
  13. 121
      com.unity.perception/Editor/Randomization/Editors/ParameterListEditor.cs
  14. 29
      com.unity.perception/Editor/Randomization/Editors/RandomParametersAssetEditor.cs
  15. 81
      com.unity.perception/Editor/Randomization/Uss/ParameterListStyles.uss
  16. 21
      com.unity.perception/Editor/Randomization/Uxml/ParameterListEditor.uxml
  17. 12
      com.unity.perception/Editor/Randomization/VisualElements/ParameterDragBar.cs
  18. 3
      com.unity.perception/Editor/Randomization/VisualElements/ParameterDragBar.cs.meta
  19. 3
      com.unity.perception/Editor/Randomization/VisualElements/ParameterDragManipulator.cs.meta
  20. 101
      com.unity.perception/Editor/Randomization/VisualElements/ParameterDragManipulator.cs
  21. 23
      com.unity.perception/Runtime/Randomization/ParameterBehaviours/RandomParametersAsset.cs
  22. 0
      /com.unity.perception/Editor/Randomization/Editors/ParametersAssetEditor.cs.meta
  23. 0
      /com.unity.perception/Editor/Randomization/Uss/ParameterAssetStyles.uss.meta
  24. 0
      /com.unity.perception/Editor/Randomization/Uxml/ParameterAssetEditor.uxml.meta
  25. 0
      /com.unity.perception/Runtime/Randomization/ParameterBehaviours/ParameterAsset.cs.meta

2
com.unity.perception/Editor/Randomization/Uss/ParameterStyles.uss


.parameter-type-label {
color: cornflowerblue;
/*min-width: 120px;*/
margin-right: 4px;
}
.parameter-type-label-box .unity-base-text-field__input {

10
com.unity.perception/Editor/Randomization/Uxml/ParameterListItemElement.uxml


<Box class="parameter-type-label-box">
<VisualElement name="collapse" class="collapse-parameter-toggle foldout-open"/>
<Label name="parameter-type-label" text="Type:" class="parameter-type-label"/>
<TextField name="name" text="Parameter Name"/>
<TextElement name="name" text="Field Name"/>
<Button name="remove-parameter" class="remove-parameter-button"/>
<Box>
<VisualElement class="unity-base-field">
<Label text="Target Property" class="unity-base-field__label"/>
<editor:ToolbarMenu text="Select A Property" name="property-select-menu" class="property-select-menu"/>
</VisualElement>
<editor:EnumField label="Application Frequency" name="application-frequency"/>
</Box>
</VisualElement>
</VisualElement>
</Box>

129
com.unity.perception/Editor/Randomization/VisualElements/ParameterListItemElement.cs


{
class ParameterListItemElement : VisualElement
{
Type m_ParameterSampleType;
SerializedProperty m_Target;
SerializedProperty m_TargetComponent;
SerializedProperty m_TargetProperty;
public ParameterListEditor configEditor { get; }
public int ParameterIndex => parent.IndexOf(this);
GameObject gameObject => ((ParameterList)m_SerializedProperty.serializedObject.targetObject).gameObject;
GameObject targetGameObject => m_TargetComponent.objectReferenceValue != null
? ((Component)m_TargetComponent.objectReferenceValue).gameObject : null;
public bool collapsed
{
get => m_Collapsed.boolValue;

}
}
public string displayName => m_SerializedProperty.displayName;
public bool filtered
{
set => style.display = value

public ParameterListItemElement(SerializedProperty property, ParameterListEditor config)
public ParameterListItemElement(SerializedProperty property)
configEditor = config;
var parameterProperty = m_SerializedProperty.FindPropertyRelative("parameter");
var parameter = (Parameter)StaticData.GetManagedReferenceValue(parameterProperty);
m_ParameterSampleType = parameter.sampleType;
m_Collapsed = parameterProperty.FindPropertyRelative("collapsed");
m_Target = m_SerializedProperty.FindPropertyRelative("target");
m_TargetComponent = m_Target.FindPropertyRelative("component");
m_TargetProperty = m_Target.FindPropertyRelative("propertyName");
this.AddManipulator(new ParameterDragManipulator());
var removeButton = this.Q<Button>("remove-parameter");
removeButton.RegisterCallback<MouseUpEvent>(evt => configEditor.RemoveParameter(this));
var parameter = (Parameter)StaticData.GetManagedReferenceValue(property);
m_Collapsed = property.FindPropertyRelative("collapsed");
var parameterNameField = this.Q<TextField>("name");
parameterNameField.isDelayed = true;
parameterNameField.BindProperty(m_SerializedProperty.FindPropertyRelative("name"));
var targetObj = targetGameObject;
if (targetObj == null)
m_TargetProperty.stringValue = string.Empty;
else if (targetObj != gameObject)
{
var component = (Component)m_TargetComponent.objectReferenceValue;
m_TargetComponent.objectReferenceValue = gameObject.GetComponent(component.GetType());
m_SerializedProperty.serializedObject.ApplyModifiedPropertiesWithoutUndo();
}
m_TargetPropertyMenu = this.Q<ToolbarMenu>("property-select-menu");
FillPropertySelectMenu();
var frequencyField = this.Q<EnumField>("application-frequency");
frequencyField.Init(ParameterApplicationFrequency.EveryIteration);
var applicationFrequency = m_Target.FindPropertyRelative("applicationFrequency");
frequencyField.BindProperty(applicationFrequency);
var parameterNameField = this.Q<TextElement>("name");
parameterNameField.text = property.displayName;
parameterProperties.Add(new ParameterElement(m_SerializedProperty.FindPropertyRelative("parameter")));
}
void SetTarget(ParameterTarget newTarget)
{
m_Target.FindPropertyRelative("component").objectReferenceValue = newTarget.component;
m_Target.FindPropertyRelative("propertyName").stringValue = newTarget.propertyName;
m_Target.FindPropertyRelative("fieldOrProperty").intValue = (int)newTarget.fieldOrProperty;
m_SerializedProperty.serializedObject.ApplyModifiedProperties();
m_TargetPropertyMenu.text = TargetPropertyDisplayText(newTarget.component, newTarget.propertyName);
}
static string TargetPropertyDisplayText(Component component, string propertyName)
{
return $"{component.GetType().Name}.{propertyName}";
}
void FillPropertySelectMenu()
{
m_TargetPropertyMenu.menu.MenuItems().Clear();
var options = GatherPropertyOptions(m_ParameterSampleType);
if (options.Count == 0)
{
m_TargetPropertyMenu.text = "No compatible properties";
m_TargetPropertyMenu.SetEnabled(false);
}
else
{
m_TargetPropertyMenu.SetEnabled(true);
foreach (var option in options)
{
m_TargetPropertyMenu.menu.AppendAction(
TargetPropertyDisplayText(option.component, option.propertyName),
a => { SetTarget(option); });
}
m_TargetPropertyMenu.text = m_TargetProperty.stringValue == string.Empty
? "Select a property"
: TargetPropertyDisplayText((Component)m_TargetComponent.objectReferenceValue, m_TargetProperty.stringValue);
}
}
List<ParameterTarget> GatherPropertyOptions(Type propertyType)
{
var obj = ((ParameterList)m_SerializedProperty.serializedObject.targetObject).gameObject;
var options = new List<ParameterTarget>();
foreach (var component in obj.GetComponents<Component>())
{
if (component == null)
continue;
var componentType = component.GetType();
var fieldInfos = componentType.GetFields();
foreach (var fieldInfo in fieldInfos)
{
if (fieldInfo.FieldType == propertyType && fieldInfo.IsPublic && !fieldInfo.IsInitOnly)
options.Add(new ParameterTarget()
{
component = component,
propertyName = fieldInfo.Name,
fieldOrProperty = FieldOrProperty.Field
});
}
var propertyInfos = componentType.GetProperties();
foreach (var propertyInfo in propertyInfos)
{
if (propertyInfo.PropertyType == propertyType && propertyInfo.GetSetMethod() != null)
options.Add(new ParameterTarget()
{
component = component,
propertyName = propertyInfo.Name,
fieldOrProperty = FieldOrProperty.Property
});
}
}
return options;
parameterProperties.Add(new ParameterElement(property));
}
}
}

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


using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Perception.Randomization.Parameters;
using UnityEngine.Perception.Randomization.Samplers;

/// </summary>
public abstract class ParameterBehaviour : MonoBehaviour
{
static HashSet<ParameterBehaviour> s_ActiveBehaviours = new HashSet<ParameterBehaviour>();
static Queue<ParameterBehaviour> s_PendingBehaviours = new Queue<ParameterBehaviour>();
internal static IEnumerable<ParameterBehaviour> behaviours
static ParameterBehaviour s_ActiveBehaviour;
internal static ParameterBehaviour activeBehaviour
get
get => s_ActiveBehaviour;
private set
var currentBehaviours = s_ActiveBehaviours.ToArray();
foreach (var behaviour in currentBehaviours)
if (s_ActiveBehaviours.Contains(behaviour))
yield return behaviour;
while (s_PendingBehaviours.Count > 0)
{
var behaviour = s_PendingBehaviours.Dequeue();
if (!s_ActiveBehaviours.Contains(behaviour))
{
s_ActiveBehaviours.Add(behaviour);
yield return behaviour;
}
}
if (value != null && s_ActiveBehaviour != null && value != s_ActiveBehaviour)
throw new Exception("There cannot be more than one active Scenario");
s_ActiveBehaviour = value;
}
}

/// </summary>
protected void OnEnable()
{
s_PendingBehaviours.Enqueue(this);
ResetState();
activeBehaviour = this;
ResetParameterRandomStates();
}
/// <summary>

{
s_PendingBehaviours.Enqueue(this);
activeBehaviour = null;
}
/// <summary>

/// <summary>
/// Resets the state of each sampler on every parameter used by this ParameterBehaviour
/// </summary>
internal void ResetState()
internal void ResetParameterRandomStates()
{
foreach (var parameter in parameters)
{

20
com.unity.perception/Runtime/Randomization/Scenarios/ScenarioBase.cs


bool m_FirstScenarioFrame = true;
bool m_WaitingForFinalUploads;
[SerializeReference] public RandomParametersAsset[] parameters;
[SerializeReference] public ParameterAsset[] parameters;
/// <summary>
/// If true, this scenario will quit the Unity application when it's finished executing

{
if (deserializeOnStart)
Deserialize();
foreach (var behaviour in ParameterBehaviour.behaviours)
behaviour.Validate();
ParameterBehaviour.activeBehaviour.Validate();
}
void Update()

{
currentIteration++;
currentIterationFrame = 0;
foreach (var behaviour in ParameterBehaviour.behaviours)
behaviour.OnIterationEnd();
ParameterBehaviour.activeBehaviour.OnIterationEnd();
}
}

foreach (var behaviour in ParameterBehaviour.behaviours)
behaviour.OnScenarioComplete();
ParameterBehaviour.activeBehaviour.OnScenarioComplete();
Manager.Instance.Shutdown();
DatasetCapture.ResetSimulation();
m_WaitingForFinalUploads = true;

if (currentIterationFrame == 0)
{
DatasetCapture.StartNewSequence();
foreach (var behaviour in ParameterBehaviour.behaviours)
behaviour.ResetState();
foreach (var behaviour in ParameterBehaviour.behaviours)
behaviour.OnIterationStart();
ParameterBehaviour.activeBehaviour.ResetParameterRandomStates();
ParameterBehaviour.activeBehaviour.OnIterationStart();
foreach (var behaviour in ParameterBehaviour.behaviours)
behaviour.OnFrameStart();
ParameterBehaviour.activeBehaviour.OnFrameStart();
}
}
}

73
com.unity.perception/Editor/Randomization/Editors/ParametersAssetEditor.cs


using System;
using Randomization.ParameterBehaviours;
using UnityEditor;
using UnityEngine.Perception.Randomization.Parameters;
using UnityEngine.UIElements;
namespace UnityEngine.Perception.Randomization.Editor
{
[CustomEditor(typeof(ParameterAsset), true)]
public class ParametersAssetEditor : UnityEditor.Editor
{
VisualElement m_Root;
VisualElement m_ParameterContainer;
SerializedProperty m_Parameters;
string m_FilterString = string.Empty;
string FilterString
{
set
{
m_FilterString = value;
var lowerFilter = m_FilterString.ToLower();
foreach (var child in m_ParameterContainer.Children())
{
var param = (ParameterListItemElement)child;
param.filtered = param.displayName.ToLower().Contains(lowerFilter);
}
}
}
public override VisualElement CreateInspectorGUI()
{
m_Root = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(
$"{StaticData.uxmlDir}/ParameterAssetEditor.uxml").CloneTree();
m_ParameterContainer = m_Root.Q<VisualElement>("parameters-container");
var filter = m_Root.Q<TextField>("filter-parameters");
filter.RegisterValueChangedCallback((e) => { FilterString = e.newValue; });
var collapseAllButton = m_Root.Q<Button>("collapse-all");
collapseAllButton.clicked += () => CollapseParameters(true);
var expandAllButton = m_Root.Q<Button>("expand-all");
expandAllButton.clicked += () => CollapseParameters(false);
RefreshParameterElements();
return m_Root;
}
void RefreshParameterElements()
{
m_ParameterContainer.Clear();
var properties = serializedObject.GetIterator();
if (properties.NextVisible(true))
{
do
{
var propertyValue = StaticData.GetManagedReferenceValue(properties);
if (propertyValue != null && propertyValue.GetType().IsSubclassOf(typeof(Parameter)))
m_ParameterContainer.Add(new ParameterListItemElement(properties.Copy()));
} while (properties.NextVisible(false));
}
}
void CollapseParameters(bool collapsed)
{
foreach (var child in m_ParameterContainer.Children())
((ParameterListItemElement)child).collapsed = collapsed;
}
}
}

46
com.unity.perception/Editor/Randomization/Uss/ParameterAssetStyles.uss


.dark-viewport {
border-radius: 5px;
background-color: #191919;
padding: 2px;
}
.unity-toggle {
margin-left: 0;
}
.remove-parameter-button {
background-image: resource("Packages/com.unity.perception/Editor/Randomization/Icons/X.png");
width: 16px;
height: 16px;
}
.move-buttons-container {
width: auto;
margin-right: 6px;
border-color: black;
border-top-left-radius: 3px;
border-bottom-left-radius: 3px;
background-color: #2A2A2A;
padding: 3px 2px;
justify-content: center;
}
.drag-handle {
width: 16px;
height: 70%;
min-height: 20px;
background-image: resource("Packages/com.unity.perception/Editor/Randomization/Icons/DragHandle.png");
}
.unity-imgui-container {
margin-left: 1px;
margin-right: 3px;
}
.search-icon {
width: 16px;
height: 16px;
margin-top: 2px;
flex-shrink: 0;
background-image: resource("Packages/com.unity.perception/Editor/Randomization/Icons/Search.png");
}

21
com.unity.perception/Editor/Randomization/Uxml/ParameterAssetEditor.uxml


<UXML xmlns="UnityEngine.UIElements" xmlns:editor="UnityEditor.UIElements">
<VisualElement style="height: 100%;">
<Style src="../Uss/ParameterAssetStyles.uss"/>
<VisualElement style="flex-direction: row; align-items: center; margin-bottom: 2px; margin-top: 2px;">
<VisualElement class="search-icon" style="margin-left: 3px; margin-right: 2px;"/>
<TextField name="filter-parameters" style="flex-grow: 1; flex-shrink: 1;"/>
</VisualElement>
<VisualElement class="dark-viewport" style="min-height: 100px;">
<VisualElement name="parameters-container" style="flex-shrink: 0;"/>
</VisualElement>
<VisualElement style="flex-direction: row; justify-content: center;">
<VisualElement style="flex-grow: 1"/>
<Button name="collapse-all" text="Collapse All" style="font-size: 13px; padding: 4px;"/>
<Button name="expand-all" text="Expand All" style="font-size: 13px; padding: 4px;"/>
<VisualElement style="flex-grow: 1"/>
</VisualElement>
</VisualElement>
</UXML>

33
com.unity.perception/Runtime/Randomization/ParameterBehaviours/ParameterAsset.cs


using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Perception.Randomization.Parameters;
using UnityEngine.Perception.Randomization.Samplers;
namespace Randomization.ParameterBehaviours
{
public abstract class ParameterAsset : ScriptableObject
{
public abstract IEnumerable<Parameter> parameters { get; }
/// <summary>
/// Reset to default values in the Editor
/// </summary>
void Reset()
{
var fields = GetType().GetFields();
foreach (var field in fields)
{
if (!field.IsPublic || !field.FieldType.IsSubclassOf(typeof(Parameter)))
continue;
var parameter = (Parameter)field.GetValue(this);
if (parameter == null)
field.SetValue(this, Activator.CreateInstance(field.FieldType));
}
foreach (var parameter in parameters)
foreach (var sampler in parameter.samplers)
sampler.baseSeed = SamplerUtility.GenerateRandomSeed();
}
}
}

30
com.unity.perception/Editor/Randomization/Editors/ParameterBehaviourEditor.cs


using System;
using Randomization.ParameterBehaviours;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine.UIElements;
namespace UnityEngine.Perception.Randomization.Editor
{
[CustomEditor(typeof(ParameterBehaviour), true)]
class ParameterBehaviourEditor : UnityEditor.Editor
{
public override VisualElement CreateInspectorGUI()
{
var root = new VisualElement();
var iterator = serializedObject.GetIterator();
// Start iterating over properties
iterator.NextVisible(true);
// Skip m_Script property
iterator.NextVisible(false);
// Create property fields
do { root.Add(new PropertyField(iterator.Copy())); }
while (iterator.NextVisible(false));
return root;
}
}
}

3
com.unity.perception/Editor/Randomization/Editors/ParameterBehaviourEditor.cs.meta


fileFormatVersion: 2
guid: 305a421aca744ce49ced7056c1733f48
timeCreated: 1598220645

11
com.unity.perception/Editor/Randomization/Editors/ParameterListEditor.cs.meta


fileFormatVersion: 2
guid: dd62abede5784c84f90495b367408ced
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

121
com.unity.perception/Editor/Randomization/Editors/ParameterListEditor.cs


using System;
using UnityEngine;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine.Perception.Randomization.ParameterBehaviours;
using UnityEngine.Perception.Randomization.Parameters;
using UnityEngine.UIElements;
namespace UnityEngine.Perception.Randomization.Editor
{
[CustomEditor(typeof(ParameterList))]
class ParameterListEditor : UnityEditor.Editor
{
VisualElement m_Root;
VisualElement m_ParameterContainer;
SerializedProperty m_Parameters;
public ParameterList config;
string m_FilterString = string.Empty;
public string FilterString
{
get => m_FilterString;
private set
{
m_FilterString = value;
var lowerFilter = m_FilterString.ToLower();
foreach (var child in m_ParameterContainer.Children())
{
var paramIndex = m_ParameterContainer.IndexOf(child);
var param = config.configuredParameters[paramIndex];
((ParameterListItemElement)child).filtered = param.name.ToLower().Contains(lowerFilter);
}
}
}
public override VisualElement CreateInspectorGUI()
{
config = (ParameterList)target;
m_Parameters = serializedObject.FindProperty("configuredParameters");
m_Root = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(
$"{StaticData.uxmlDir}/ParameterListEditor.uxml").CloneTree();
m_ParameterContainer = m_Root.Q<VisualElement>("parameters-container");
var filter = m_Root.Q<TextField>("filter-parameters");
filter.RegisterValueChangedCallback((e) => { FilterString = e.newValue; });
var collapseAllButton = m_Root.Q<Button>("collapse-all");
collapseAllButton.clicked += () => CollapseParameters(true);
var expandAllButton = m_Root.Q<Button>("expand-all");
expandAllButton.clicked += () => CollapseParameters(false);
var parameterTypeMenu = m_Root.Q<ToolbarMenu>("parameter-type-menu");
foreach (var parameterType in StaticData.parameterTypes)
{
parameterTypeMenu.menu.AppendAction(
Parameter.GetDisplayName(parameterType),
a => { AddParameter(parameterType); },
a => DropdownMenuAction.Status.Normal);
}
RefreshParameterElements();
return m_Root;
}
void RefreshParameterElements()
{
m_ParameterContainer.Clear();
for (var i = 0; i < m_Parameters.arraySize; i++)
m_ParameterContainer.Add(new ParameterListItemElement(m_Parameters.GetArrayElementAtIndex(i), this));
}
void AddParameter(Type parameterType)
{
var configuredParameter = config.AddParameter($"Parameter{m_Parameters.arraySize}", parameterType);
configuredParameter.parameter.RandomizeSamplers();
serializedObject.Update();
RefreshParameterElements();
}
public void RemoveParameter(VisualElement template)
{
var paramIndex = m_ParameterContainer.IndexOf(template);
m_ParameterContainer.RemoveAt(paramIndex);
config.configuredParameters.RemoveAt(paramIndex);
serializedObject.Update();
RefreshParameterElements();
}
public void ReorderParameter(int currentIndex, int nextIndex)
{
if (currentIndex == nextIndex)
return;
if (nextIndex > currentIndex)
nextIndex--;
var parameterElement = m_ParameterContainer[currentIndex];
var parameter = config.configuredParameters[currentIndex];
parameterElement.RemoveFromHierarchy();
config.configuredParameters.RemoveAt(currentIndex);
m_ParameterContainer.Insert(nextIndex, parameterElement);
config.configuredParameters.Insert(nextIndex, parameter);
serializedObject.Update();
RefreshParameterElements();
}
void CollapseParameters(bool collapsed)
{
foreach (var child in m_ParameterContainer.Children())
((ParameterListItemElement)child).collapsed = collapsed;
}
}
}

29
com.unity.perception/Editor/Randomization/Editors/RandomParametersAssetEditor.cs


using Randomization.ParameterBehaviours;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine.UIElements;
namespace UnityEngine.Perception.Randomization.Editor
{
[CustomEditor(typeof(RandomParametersAsset), true)]
public class RandomParametersAssetEditor : UnityEditor.Editor
{
public override VisualElement CreateInspectorGUI()
{
var root = new VisualElement();
var iterator = serializedObject.GetIterator();
// Start iterating over properties
iterator.NextVisible(true);
// Skip m_Script property
iterator.NextVisible(false);
// Create property fields
do { root.Add(new PropertyField(iterator.Copy())); }
while (iterator.NextVisible(false));
return root;
}
}
}

81
com.unity.perception/Editor/Randomization/Uss/ParameterListStyles.uss


.dark-viewport {
border-radius: 5px;
background-color: #191919;
padding: 2px;
}
.unity-toggle {
margin-left: 0;
}
.property-select-menu {
flex-grow: 1;
border-width: 1px;
padding-bottom: 1px;
border-radius: 3px;
margin-right: 1px;
}
.remove-parameter-button {
background-image: resource("Packages/com.unity.perception/Editor/Randomization/Icons/X.png");
width: 16px;
height: 16px;
}
.move-buttons-container {
width: auto;
margin-right: 6px;
border-color: black;
border-top-left-radius: 3px;
border-bottom-left-radius: 3px;
background-color: #2A2A2A;
padding: 3px 2px;
justify-content: center;
}
.drag-handle {
width: 16px;
height: 70%;
min-height: 20px;
background-image: resource("Packages/com.unity.perception/Editor/Randomization/Icons/DragHandle.png");
}
.move-button {
border-width: 0;
width: 42px;
height: 16px;
padding: 9px;
-unity-background-scale-mode: scale-to-fit;
}
.unity-imgui-container {
margin-left: 1px;
margin-right: 3px;
}
.search-icon {
width: 16px;
height: 16px;
margin-top: 2px;
flex-shrink: 0;
background-image: resource("Packages/com.unity.perception/Editor/Randomization/Icons/Search.png");
}
.parameter-drag-bar {
width: 100px;
height: 6px;
background-color: rgba(100,149,237,0.4);
position: absolute;
}
.parameter-type-menu {
margin: 1px 3px;
border-width: 1px;
border-radius: 3px;
background-color: #585858;
}
#parameter-type-menu .unity-toolbar-menu__text {
font-size: 13px;
padding: 3px;
}

21
com.unity.perception/Editor/Randomization/Uxml/ParameterListEditor.uxml


<UXML xmlns="UnityEngine.UIElements" xmlns:editor="UnityEditor.UIElements">
<VisualElement>
<Style src="../Uss/ParameterListStyles.uss"/>
<VisualElement style="flex-direction: row; align-items: center; margin-bottom: 2px; margin-top: 2px;">
<VisualElement class="search-icon" style="margin-left: 3px; margin-right: 2px;"/>
<TextField name="filter-parameters" style="flex-grow: 1; flex-shrink: 1;"/>
</VisualElement>
<ScrollView name="parameter-scroll-view" class="dark-viewport" style="min-height: 100px; max-height: 600px;">
<VisualElement name="parameters-container" style="flex-shrink: 0;"/>
</ScrollView>
<VisualElement style="flex-direction: row; justify-content: space-between;">
<VisualElement style="flex-grow: 1"/>
<editor:ToolbarMenu text="Add New Parameter" name="parameter-type-menu" class="parameter-type-menu"/>
<Button name="collapse-all" text="Collapse All" style="font-size: 13px; padding: 4px;"/>
<Button name="expand-all" text="Expand All" style="font-size: 13px; padding: 4px;"/>
<VisualElement style="flex-grow: 1"/>
</VisualElement>
</VisualElement>
</UXML>

12
com.unity.perception/Editor/Randomization/VisualElements/ParameterDragBar.cs


using UnityEngine.UIElements;
namespace UnityEngine.Perception.Randomization.Editor
{
class ParameterDragBar : VisualElement
{
public ParameterDragBar()
{
AddToClassList("parameter-drag-bar");
}
}
}

3
com.unity.perception/Editor/Randomization/VisualElements/ParameterDragBar.cs.meta


fileFormatVersion: 2
guid: 7c1e08b02e5a4c55875f34baf32f8e76
timeCreated: 1596143672

3
com.unity.perception/Editor/Randomization/VisualElements/ParameterDragManipulator.cs.meta


fileFormatVersion: 2
guid: f2b59fa8baf440f597257d8eb8219afa
timeCreated: 1596140810

101
com.unity.perception/Editor/Randomization/VisualElements/ParameterDragManipulator.cs


using UnityEngine.UIElements;
namespace UnityEngine.Perception.Randomization.Editor
{
class ParameterDragManipulator : MouseManipulator
{
bool m_Active;
float m_Offset;
ParameterListItemElement m_ParameterListItemElement;
VisualElement m_DragHandle;
VisualElement m_DragBar;
VisualElement m_ParameterContainer;
protected override void RegisterCallbacksOnTarget()
{
m_ParameterListItemElement = (ParameterListItemElement)target;
m_DragHandle = m_ParameterListItemElement.Q<VisualElement>("drag-handle");
m_DragHandle.RegisterCallback<MouseDownEvent>(OnMouseDown);
m_DragHandle.RegisterCallback<MouseMoveEvent>(OnMouseMove);
m_DragHandle.RegisterCallback<MouseUpEvent>(OnMouseUp);
}
protected override void UnregisterCallbacksFromTarget()
{
m_DragHandle.UnregisterCallback<MouseDownEvent>(OnMouseDown);
m_DragHandle.UnregisterCallback<MouseMoveEvent>(OnMouseMove);
m_DragHandle.UnregisterCallback<MouseUpEvent>(OnMouseUp);
}
void OnMouseDown(MouseDownEvent evt)
{
if (m_Active)
{
evt.StopImmediatePropagation();
return;
}
if (m_ParameterListItemElement.configEditor.FilterString != string.Empty)
return;
m_ParameterContainer = target.parent;
m_DragBar = new ParameterDragBar();
m_DragBar.style.width = new StyleLength(m_ParameterContainer.resolvedStyle.width);
target.parent.Add(m_DragBar);
m_Offset = m_DragHandle.worldBound.position.y - m_ParameterContainer.worldBound.position.y;
m_DragBar.style.top = evt.localMousePosition.y + m_Offset;
m_Active = true;
m_DragHandle.CaptureMouse();
evt.StopPropagation();
}
void OnMouseMove(MouseMoveEvent evt)
{
if (!m_Active || !m_DragHandle.HasMouseCapture())
return;
m_DragBar.style.top = evt.localMousePosition.y + m_Offset;
evt.StopPropagation();
}
void OnMouseUp(MouseUpEvent evt)
{
if (!m_Active || !m_DragHandle.HasMouseCapture() || !CanStopManipulation(evt))
return;
var dragBarY = evt.localMousePosition.y + m_Offset;
m_DragBar.RemoveFromHierarchy();
m_Active = false;
m_DragHandle.ReleaseMouse();
evt.StopPropagation();
var p = 0;
var middlePoints = new float[m_ParameterContainer.childCount];
foreach (var parameterElement in m_ParameterContainer.Children())
{
var middleHeight = parameterElement.worldBound.height / 2;
var localY = parameterElement.worldBound.y - m_ParameterContainer.worldBound.position.y;
middlePoints[p++] = middleHeight + localY;
}
for (var i = 0; i < middlePoints.Length; i++)
{
if (dragBarY < middlePoints[i])
{
ReorderParameter(m_ParameterListItemElement.ParameterIndex, i);
return;
}
}
ReorderParameter(m_ParameterListItemElement.ParameterIndex, middlePoints.Length);
}
void ReorderParameter(int currentIndex, int nextIndex)
{
m_ParameterListItemElement.configEditor.ReorderParameter(currentIndex, nextIndex);
}
}
}

23
com.unity.perception/Runtime/Randomization/ParameterBehaviours/RandomParametersAsset.cs


using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Perception.Randomization.Parameters;
using UnityEngine.Perception.Randomization.Samplers;
namespace Randomization.ParameterBehaviours
{
public abstract class RandomParametersAsset : ScriptableObject
{
public abstract IEnumerable<Parameter> parameters { get; }
/// <summary>
/// Reset to default values in the Editor
/// </summary>
void Reset()
{
foreach (var parameter in parameters)
foreach (var sampler in parameter.samplers)
sampler.baseSeed = SamplerUtility.GenerateRandomSeed();
}
}
}

/com.unity.perception/Editor/Randomization/Editors/RandomParametersAssetEditor.cs.meta → /com.unity.perception/Editor/Randomization/Editors/ParametersAssetEditor.cs.meta

/com.unity.perception/Editor/Randomization/Uss/ParameterListStyles.uss.meta → /com.unity.perception/Editor/Randomization/Uss/ParameterAssetStyles.uss.meta

/com.unity.perception/Editor/Randomization/Uxml/ParameterListEditor.uxml.meta → /com.unity.perception/Editor/Randomization/Uxml/ParameterAssetEditor.uxml.meta

/com.unity.perception/Runtime/Randomization/ParameterBehaviours/RandomParametersAsset.cs.meta → /com.unity.perception/Runtime/Randomization/ParameterBehaviours/ParameterAsset.cs.meta

正在加载...
取消
保存