浏览代码

Fixing error with non-public serialized fields on randomizers (#211)

* Fixing bug in UIElementsEditorUtilities and adding tests

* Adding error message when missing scripts are detected

* Update com.unity.perception/CHANGELOG.md

Co-authored-by: Mohsen K <mohsen.kamalzadeh@unity3d.com>

Co-authored-by: Mohsen K <mohsen.kamalzadeh@unity3d.com>
/main
GitHub 4 年前
当前提交
65841343
共有 7 个文件被更改,包括 87 次插入3 次删除
  1. 4
      com.unity.perception/CHANGELOG.md
  2. 4
      com.unity.perception/Editor/Randomization/Uss/Styles.uss
  3. 5
      com.unity.perception/Editor/Randomization/Utilities/UIElementsEditorUtilities.cs
  4. 6
      com.unity.perception/Editor/Randomization/VisualElements/Randomizer/RandomizerElement.cs
  5. 13
      com.unity.perception/Editor/Randomization/VisualElements/Randomizer/RandomizerList.cs
  6. 47
      com.unity.perception/Tests/Editor/UIElementsEditorUtilitiesTests.cs
  7. 11
      com.unity.perception/Tests/Editor/UIElementsEditorUtilitiesTests.cs.meta

4
com.unity.perception/CHANGELOG.md


### Added
Added error message when missing Randomizer scripts are detected
Scenario serialization has been updated to include scalar values on randomizers and parameters.
Added new ScenarioBase virtual lifecycle hooks: OnAwake, OnStart, OnComplete, and OnIdle.

Fixed a null reference error that appeared when adding options to categorical parameters.
Fixed ground truth not properly produced when there are other disabled PerceptionCameras present. Note: this does not yet add support for multiple enabled PerceptionCameras.
Fixed exception when rendering inspector for randomizers with private serialized fields
Fixed an issue preventing a user from adding more options to a Categorical Parameter's list of options with the 'Add Folder' button. 'Add Folder' now correctly appends the contents of the new folder on the list.

4
com.unity.perception/Editor/Randomization/Uss/Styles.uss


margin: 3px 3px 3px 3px;
}
.scenario__error-box {
color: #FF4040;
}
.scenario__dark-viewport {
border-radius: 5px;
background-color: #191919;

5
com.unity.perception/Editor/Randomization/Utilities/UIElementsEditorUtilities.cs


using System;
using System.Linq;
using System.Reflection;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.Perception.Randomization.Randomizers.SampleRandomizers;

/// <summary>
/// This class contains a set of helper functions for simplifying the creation of UI Elements editors
/// </summary>
public static class UIElementsEditorUtilities
static class UIElementsEditorUtilities
{
/// <summary>
/// Creates a list of PropertyFields from the class fields of the given SerializedObject

{
var propertyField = new PropertyField(iterator.Copy());
propertyField.Bind(iterator.serializedObject);
var originalField = parentPropertyType.GetField(iterator.name);
var originalField = parentPropertyType.GetField(iterator.name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance );
var tooltipAttribute = originalField.GetCustomAttributes(true)
.ToList().Find(att => att.GetType() == typeof(TooltipAttribute));
if (tooltipAttribute != null)

6
com.unity.perception/Editor/Randomization/VisualElements/Randomizer/RandomizerElement.cs


{
m_Property = property;
this.randomizerList = randomizerList;
m_Collapsed = property.FindPropertyRelative("collapsed");
collapsed = m_Collapsed.boolValue;

public bool collapsed
{
get => m_Collapsed.boolValue;
get => m_Collapsed?.boolValue ?? true;
if (m_Collapsed == null)
return;
m_Collapsed.boolValue = value;
m_Property.serializedObject.ApplyModifiedPropertiesWithoutUndo();
if (value)

13
com.unity.perception/Editor/Randomization/VisualElements/Randomizer/RandomizerList.cs


void RefreshList()
{
m_Container.Clear();
if (m_Property.arraySize > 0 &&
string.IsNullOrEmpty(m_Property.GetArrayElementAtIndex(0).managedReferenceFullTypename))
{
var textElement = new TextElement()
{
text = "One or more randomizers have missing scripts. See console for more info."
};
textElement.AddToClassList("scenario__info-box");
textElement.AddToClassList("scenario__error-box");
m_Container.Add(textElement);
return;
}
for (var i = 0; i < m_Property.arraySize; i++)
m_Container.Add(new RandomizerElement(m_Property.GetArrayElementAtIndex(i), this));
}

47
com.unity.perception/Tests/Editor/UIElementsEditorUtilitiesTests.cs


using System;
using NUnit.Framework;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;
public class UIElementsEditorUtilitiesTests
{
//ScriptableObject so we can use ScriptableObject.CreateInstance()
[Serializable]
class TestType : ScriptableObject
{
public string publicField;
[Tooltip("A tooltip")]
public string publicFieldWithTooltip;
[Tooltip("A tooltip")]
[SerializeField]
private string privateField;
[Tooltip("A tooltip")]
[SerializeField]
protected string protectedField;
[Tooltip("A tooltip")]
[SerializeField]
protected internal string protectedInternalField;
[Tooltip("A tooltip")]
[SerializeField]
internal string internalField;
}
[Test]
[TestCase(typeof(TestType), nameof(TestType.publicField))]
[TestCase(typeof(TestType), nameof(TestType.publicFieldWithTooltip), "A tooltip")]
[TestCase(typeof(TestType), "privateField", "A tooltip")]
[TestCase(typeof(TestType), "protectedField", "A tooltip")]
[TestCase(typeof(TestType), "protectedInternalField", "A tooltip")]
[TestCase(typeof(TestType), "internalField", "A tooltip")]
public void CreatePropertyField_ReturnsCorrectPropertyField_ForTypeAndField(Type type, string field,
string tooltipExpected = "")
{
var testType = ScriptableObject.CreateInstance<TestType>();
var serializedObject = new SerializedObject(testType);
var serializedProperty = serializedObject.FindProperty(field);
Assert.IsNotNull(serializedProperty);
var propertyField = UnityEditor.Perception.Randomization.UIElementsEditorUtilities.CreatePropertyField(serializedProperty, type);
Assert.IsNotNull(propertyField);
Assert.AreEqual(tooltipExpected, propertyField.tooltip);
}
}

11
com.unity.perception/Tests/Editor/UIElementsEditorUtilitiesTests.cs.meta


fileFormatVersion: 2
guid: 431b0b04c9f64b442b5d704e0cea4ce0
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存