浏览代码

Merge pull request #169 from Unity-Technologies/generic-inspector-utility

Added a default UI Elements inspector editor class
/main
GitHub 4 年前
当前提交
449fe78c
共有 6 个文件被更改,包括 66 次插入1 次删除
  1. 1
      com.unity.perception/CHANGELOG.md
  2. 27
      com.unity.perception/Documentation~/Randomization/Parameters.md
  3. 3
      com.unity.perception/Editor/Utilities.meta
  4. 3
      com.unity.perception/Editor/Utilities/ParameterUIElementsEditor.cs.meta
  5. 33
      com.unity.perception/Editor/Utilities/ParameterUIElementsEditor.cs

1
com.unity.perception/CHANGELOG.md


Added AnimationCurveSampler, which returns random values according to a range and probability distribution denoted by a user provided AnimationCurve.
Added ParameterUIElementsEditor class to allow custom ScriptableObjects and MonoBehaviours to render Parameter and Sampler typed public fields correctly in their inspector windows.
### Changed

27
com.unity.perception/Documentation~/Randomization/Parameters.md


1. `CategoricalParameter`
2. `NumericParameter`
## Using Parameters outside of Randomizers (ie: in MonoBehaviours and ScriptableObjects)
After adding a public Parameter field to a MonoBehaviour or ScriptableObject, you may have noticed that the Parameter's UI doesn't look the same as it does when added to a Randomizer. This is because the Inspector UI for most Perception randomization components is authored using Unity's relatively new UI Elements framework, though by default, Unity uses the old IMGUI framework to render default inspector editors.
Say you have the following CustomMonoBehaviour that has a public GameObjectParameter field:
```
using UnityEngine;
using UnityEngine.Experimental.Perception.Randomization.Parameters;
public class CustomMonoBehaviour : MonoBehaviour
{
public GameObjectParameter prefabs;
}
```
To force Unity to use UI Elements to render your CustomMonoBehaviour's inspector window, create a custom editor for your MonoBehaviour by deriving the ParameterUIElementsEditor class like so:
```
using UnityEditor;
using UnityEngine.Experimental.Perception.Editor;
[CustomEditor(typeof(CustomMonoBehaviour))]
public class TestClusterEditor : DefaultUIElementsEditor { }
```
### Categorical Parameters
Categorical parameters choose a value from a list of options that have no intrinsic ordering. For example, a material paramater randomly chooses from a list of material options, but the list of material options itself can be rearranged into any particular order without affecting the distribution of materials selected.

### Numeric Parameters
Numeric parameters use samplers to generate randomized structs. Take a look at the [ColorHsvaParameter]() class included in the perception package for an example on how to implement a numeric parameter.
Numeric parameters use samplers to generate randomized structs. Take a look at the [ColorHsvaParameter]() class included in the perception package for an example on how to implement a numeric parameter.

3
com.unity.perception/Editor/Utilities.meta


fileFormatVersion: 2
guid: 7c33b05b860544a5a026b97302d0358e
timeCreated: 1611791860

3
com.unity.perception/Editor/Utilities/ParameterUIElementsEditor.cs.meta


fileFormatVersion: 2
guid: ec638a81755645739ca5834e2e44fc13
timeCreated: 1611791890

33
com.unity.perception/Editor/Utilities/ParameterUIElementsEditor.cs


using UnityEditor.UIElements;
using UnityEngine.UIElements;
namespace UnityEngine.Experimental.Perception.Editor
{
/// <summary>
/// Derive this class to force the Unity Editor to render the default inspector using UIElements for an Object that includes a Parameter field.
/// to allow parameter UIs to render properly
/// </summary>
public abstract class ParameterUIElementsEditor : UnityEditor.Editor
{
public override VisualElement CreateInspectorGUI()
{
var rootElement = new VisualElement();
CreatePropertyFields(rootElement);
return rootElement;
}
void CreatePropertyFields(VisualElement rootElement)
{
var iterator = serializedObject.GetIterator();
iterator.NextVisible(true);
do
{
if (iterator.name == "m_Script")
continue;
var propertyField = new PropertyField(iterator.Copy());
propertyField.Bind(serializedObject);
rootElement.Add(propertyField);
} while (iterator.NextVisible(false));
}
}
}
正在加载...
取消
保存