Skip to content

Commit 3b8d32e

Browse files
committed
updated and fixed the categorical parameter UI
1 parent fa3f4ed commit 3b8d32e

File tree

5 files changed

+70
-14
lines changed

5 files changed

+70
-14
lines changed

com.unity.perception/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ Added new capture options to Perception Camera:
2727
* Can now render intermediate frames between captures.
2828
* Capture can now be triggered manually using a function call, instead of automatic capturing on a schedule.
2929

30+
Categorical Parameters will now validate that their specified options are unique at runtime
31+
3032
### Changed
3133

3234
Randomizers now access their parent scenario through the static activeScenario property
@@ -45,6 +47,7 @@ RandomizerTagManager.Query<T>() now returns RandomizerTags directly instead of t
4547

4648
Semantic Segmentation Labeler now places data in folders with randomized filenames
4749

50+
The uniform toggle on Categorical Parameters will now reset the parameter's probability weights to be uniform
4851

4952
### Deprecated
5053

@@ -74,6 +77,8 @@ Semantic Segmentation Labeler now produces output in the proper form for distrib
7477

7578
Texture Randomizer is now compatible with HDRP
7679

80+
Categorical Parameters no longer error when deleting items from long options lists
81+
7782

7883
## [0.6.0-preview.1] - 2020-12-03
7984

com.unity.perception/Editor/Randomization/VisualElements/Parameter/CategoricalOptionElement.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,26 @@ internal CategoricalOptionElement(
1717
{
1818
m_CategoryProperty = categoryProperty;
1919
m_ProbabilitiesProperty = probabilitiesProperty;
20-
21-
var template = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(
22-
$"{StaticData.uxmlDir}/Parameter/CategoricalOptionElement.uxml");
23-
template.CloneTree(this);
2420
}
2521

2622
// Called from categorical parameter
2723
public void BindProperties(int i)
2824
{
25+
// Reset this categorical item's UI
26+
Clear();
27+
var template = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(
28+
$"{StaticData.uxmlDir}/Parameter/CategoricalOptionElement.uxml");
29+
template.CloneTree(this);
30+
2931
m_Index = i;
3032
var indexLabel = this.Q<Label>("index-label");
3133
indexLabel.text = $"[{m_Index}]";
3234

3335
var optionProperty = m_CategoryProperty.GetArrayElementAtIndex(i);
3436
var option = this.Q<PropertyField>("option");
3537
option.BindProperty(optionProperty);
38+
39+
// Remove the redundant element label to save space
3640
var label = option.Q<Label>();
3741
label.parent.Remove(label);
3842

com.unity.perception/Editor/Randomization/VisualElements/Parameter/ParameterElement.cs

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,17 @@ void BindItem(VisualElement e, int i)
105105
{
106106
probabilitiesProperty.arraySize++;
107107
optionsProperty.arraySize++;
108+
var newOption = optionsProperty.GetArrayElementAtIndex(optionsProperty.arraySize - 1);
109+
switch (newOption.propertyType)
110+
{
111+
case SerializedPropertyType.ObjectReference:
112+
newOption.objectReferenceValue = null;
113+
break;
114+
case SerializedPropertyType.String:
115+
newOption.stringValue = string.Empty;
116+
break;
117+
}
118+
108119
m_SerializedProperty.serializedObject.ApplyModifiedProperties();
109120
listView.itemsSource = categoricalParameter.probabilities;
110121
listView.Refresh();
@@ -162,18 +173,33 @@ void BindItem(VisualElement e, int i)
162173
var uniformToggle = template.Q<Toggle>("uniform");
163174
var uniformProperty = m_SerializedProperty.FindPropertyRelative("uniform");
164175
uniformToggle.BindProperty(uniformProperty);
165-
void ToggleProbabilityFields(bool toggle)
166-
{
167-
if (toggle)
168-
listView.AddToClassList("collapsed");
169-
else
170-
listView.RemoveFromClassList("collapsed");
171-
}
172-
ToggleProbabilityFields(uniformToggle.value);
176+
177+
if (uniformToggle.value)
178+
listView.AddToClassList("collapsed");
179+
else
180+
listView.RemoveFromClassList("collapsed");
181+
173182
if (Application.isPlaying)
174183
uniformToggle.SetEnabled(false);
175184
else
176-
uniformToggle.RegisterCallback<ChangeEvent<bool>>(evt => ToggleProbabilityFields(evt.newValue));
185+
{
186+
uniformToggle.RegisterCallback<ChangeEvent<bool>>(evt =>
187+
{
188+
listView.ToggleInClassList("collapsed");
189+
if (!evt.newValue)
190+
return;
191+
var numOptions = optionsProperty.arraySize;
192+
var uniformProbabilityValue = 1f / numOptions;
193+
for (var i = 0; i < numOptions; i++)
194+
{
195+
var probability = probabilitiesProperty.GetArrayElementAtIndex(i);
196+
probability.floatValue = uniformProbabilityValue;
197+
}
198+
m_SerializedProperty.serializedObject.ApplyModifiedProperties();
199+
listView.itemsSource = categoricalParameter.probabilities;
200+
listView.Refresh();
201+
});
202+
}
177203

178204
m_PropertiesContainer.Add(template);
179205
}

com.unity.perception/Runtime/Randomization/Parameters/CategoricalParameter.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,20 @@ void AddOption(T option, float probability)
9797
public override void Validate()
9898
{
9999
base.Validate();
100+
101+
// Check for a non-zero amount of specified categories
100102
if (m_Categories.Count == 0)
101103
throw new ParameterValidationException("No options added to categorical parameter");
104+
105+
// Check for duplicate categories
106+
var uniqueCategories = new HashSet<T>();
107+
foreach (var option in m_Categories)
108+
if (uniqueCategories.Contains(option))
109+
throw new ParameterValidationException("Duplicate categories");
110+
else
111+
uniqueCategories.Add(option);
112+
113+
// Check if the number of specified probabilities is different from the number of listed categories
102114
if (!uniform)
103115
{
104116
if (probabilities.Count != m_Categories.Count)

com.unity.perception/Tests/Runtime/Randomization/ParameterTests/CategoricalParameterTests.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using NUnit.Framework;
22
using UnityEngine;
3+
using UnityEngine.Experimental.Perception.Editor;
34
using UnityEngine.Experimental.Perception.Randomization.Parameters;
45

56
namespace RandomizationTests.ParameterTests
@@ -25,12 +26,20 @@ public void TearDown()
2526
public void NegativeProbabilities()
2627
{
2728
var parameter = new StringParameter();
28-
var optionsArray = new [] { ("option1", 1f), ("option1", -1f) };
29+
var optionsArray = new [] { ("option1", 1f), ("option2", -1f) };
2930
Assert.Throws<ParameterValidationException>(() => parameter.SetOptions(optionsArray));
3031
}
3132

3233
[Test]
3334
public void ZeroSumProbability()
35+
{
36+
var parameter = new StringParameter();
37+
var optionsArray = new [] { ("option1", 0f), ("option2", 0f) };
38+
Assert.Throws<ParameterValidationException>(() => parameter.SetOptions(optionsArray));
39+
}
40+
41+
[Test]
42+
public void DuplicateCategoriesTest()
3443
{
3544
var parameter = new StringParameter();
3645
var optionsArray = new [] { ("option1", 0f), ("option1", 0f) };

0 commit comments

Comments
 (0)