Steven Leal
4 年前
当前提交
816a04b6
共有 18 个文件被更改,包括 97 次插入 和 281 次删除
-
4com.unity.perception/Editor/Randomization/CategoricalOptionElement.cs
-
2com.unity.perception/Editor/Randomization/FloatRangeElement.cs
-
2com.unity.perception/Editor/Randomization/ParameterConfigurationEditor.cs
-
2com.unity.perception/Editor/Randomization/ParameterDragBar.cs
-
2com.unity.perception/Editor/Randomization/ParameterDragManipulator.cs
-
4com.unity.perception/Editor/Randomization/ParameterElement.cs
-
2com.unity.perception/Editor/Randomization/RandomSeedField.cs
-
2com.unity.perception/Editor/Randomization/SamplerElement.cs
-
2com.unity.perception/Editor/Randomization/ScenarioBaseEditor.cs
-
10com.unity.perception/Editor/Randomization/StaticData.cs
-
53com.unity.perception/Runtime/Randomization/Configuration/ParameterConfiguration.cs
-
71com.unity.perception/Runtime/Randomization/Parameters/CategoricalParameter.cs
-
6com.unity.perception/Runtime/Randomization/Parameters/NumericParameter.cs
-
6com.unity.perception/Runtime/Randomization/Parameters/Parameter.cs
-
88com.unity.perception/Editor/Randomization/ParameterDrawer.cs
-
3com.unity.perception/Editor/Randomization/ParameterDrawer.cs.meta
-
116com.unity.perception/Editor/Randomization/ParameterDrawerElement.cs
-
3com.unity.perception/Editor/Randomization/ParameterDrawerElement.cs.meta
|
|||
/* |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using UnityEditor; |
|||
using UnityEngine.Perception.Randomization.Parameters; |
|||
using UnityEngine.Perception.Randomization.Parameters.Attributes; |
|||
using UnityEngine.UIElements; |
|||
|
|||
namespace UnityEngine.Perception.Randomization.Editor |
|||
{ |
|||
[CustomPropertyDrawer(typeof(Parameter), true)] |
|||
public class ParameterDrawer : PropertyDrawer |
|||
{ |
|||
List<Parameter> m_Parameters; |
|||
string[] m_Options; |
|||
int m_SelectedOptionIndex; |
|||
|
|||
public override VisualElement CreatePropertyGUI(SerializedProperty property) |
|||
{ |
|||
return new ParameterDrawerElement(property, fieldInfo); |
|||
} |
|||
|
|||
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) |
|||
{ |
|||
GatherParameterOptions(); |
|||
GetParameterOptionIndex(property); |
|||
|
|||
EditorGUI.BeginProperty(position, label, property); |
|||
var originalOption = m_SelectedOptionIndex; |
|||
m_SelectedOptionIndex = EditorGUI.Popup(position, fieldInfo.Name, m_SelectedOptionIndex, m_Options); |
|||
if (originalOption != m_SelectedOptionIndex) |
|||
{ |
|||
property.objectReferenceValue = m_SelectedOptionIndex == 0 |
|||
? null |
|||
: m_Parameters[m_SelectedOptionIndex - 1]; |
|||
property.serializedObject.ApplyModifiedProperties(); |
|||
} |
|||
EditorGUI.EndProperty(); |
|||
} |
|||
|
|||
void GatherParameterOptions() |
|||
{ |
|||
var parameterType = fieldInfo.FieldType; |
|||
|
|||
m_Parameters = new List<Parameter>(); |
|||
|
|||
if (parameterType == typeof(Parameter)) |
|||
m_Parameters = Resources.FindObjectsOfTypeAll<Parameter>().ToList(); |
|||
else |
|||
{ |
|||
var genericParameters = Resources.FindObjectsOfTypeAll<Parameter>(); |
|||
foreach (var parameter in genericParameters) |
|||
{ |
|||
if (parameter.GetType() == parameterType) |
|||
m_Parameters.Add(parameter); |
|||
} |
|||
} |
|||
m_Parameters.Sort((p1, p2) => p1.parameterName.CompareTo(p2.parameterName)); |
|||
|
|||
m_Options = new string[m_Parameters.Count + 1]; |
|||
m_Options[0] = "None"; |
|||
for (var i = 1; i <= m_Parameters.Count; i++) |
|||
{ |
|||
var parameter = m_Parameters[i - 1]; |
|||
var metadata = ParameterMetaData.GetMetaData(parameter.GetType()); |
|||
m_Options[i] = $"{parameter.parameterName} ({metadata.typeDisplayName})"; |
|||
} |
|||
} |
|||
|
|||
void GetParameterOptionIndex(SerializedProperty property) |
|||
{ |
|||
var selectedParameter = property.objectReferenceValue; |
|||
if (selectedParameter != null) |
|||
{ |
|||
for (var i = 0; i < m_Parameters.Count; i++) |
|||
if (m_Parameters[i].GetInstanceID() == selectedParameter.GetInstanceID()) |
|||
{ |
|||
m_SelectedOptionIndex = i + 1; |
|||
break; |
|||
} |
|||
} |
|||
else |
|||
m_SelectedOptionIndex = 0; |
|||
} |
|||
} |
|||
} |
|||
*/ |
|
|||
fileFormatVersion: 2 |
|||
guid: 4723714765b74b33850e58baa73bf9c2 |
|||
timeCreated: 1595428457 |
|
|||
/* |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Reflection; |
|||
using UnityEditor; |
|||
using UnityEditor.UIElements; |
|||
using UnityEngine.Perception.Randomization.Parameters; |
|||
using UnityEngine.Perception.Randomization.Parameters.Attributes; |
|||
using UnityEngine.UIElements; |
|||
|
|||
namespace UnityEngine.Perception.Randomization.Editor |
|||
{ |
|||
public class ParameterDrawerElement : BindableElement |
|||
{ |
|||
SerializedProperty m_Property; |
|||
FieldInfo m_FieldInfo; |
|||
ToolbarMenu m_ParameterMenu; |
|||
|
|||
public ParameterDrawerElement(SerializedProperty property, FieldInfo fieldInfo) |
|||
{ |
|||
m_Property = property; |
|||
m_FieldInfo = fieldInfo; |
|||
var template = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>( |
|||
$"{StaticData.uxmlDir}/ParameterDrawerElement.uxml"); |
|||
template.CloneTree(this); |
|||
|
|||
var propertyLabel = this.Q<Label>(); |
|||
propertyLabel.text = property.displayName; |
|||
m_ParameterMenu = this.Q<ToolbarMenu>(); |
|||
binding = new ParameterDrawerBinding(this); |
|||
} |
|||
|
|||
string GetSelectedOptionText() |
|||
{ |
|||
var parameter = m_Property.objectReferenceValue as Parameter; |
|||
if (parameter == null) |
|||
return "None"; |
|||
return DisplayName(parameter); |
|||
} |
|||
|
|||
static string DisplayName(Parameter parameter) |
|||
{ |
|||
return $"{parameter.parameterName} ({parameter.MetaData.typeDisplayName})"; |
|||
} |
|||
|
|||
void UpdateMenuOptions() |
|||
{ |
|||
m_ParameterMenu.menu.MenuItems().Clear(); |
|||
var parameters = GatherParameterOptions(); |
|||
var options = GetStringOptions(parameters); |
|||
for (var i = 0; i < options.Length; i++) |
|||
{ |
|||
var index = i; |
|||
var option = options[i]; |
|||
m_ParameterMenu.menu.AppendAction(option, action => |
|||
{ |
|||
m_Property.objectReferenceValue = option == "None" ? null : parameters[index - 1]; |
|||
m_Property.serializedObject.ApplyModifiedProperties(); |
|||
m_ParameterMenu.text = GetSelectedOptionText(); |
|||
}); |
|||
} |
|||
} |
|||
|
|||
Parameter[] GatherParameterOptions() |
|||
{ |
|||
var parameterType = m_FieldInfo.FieldType; |
|||
var parameters = new List<Parameter>(); |
|||
|
|||
if (parameterType == typeof(Parameter)) |
|||
parameters = Resources.FindObjectsOfTypeAll<Parameter>().ToList(); |
|||
else |
|||
{ |
|||
var genericParameters = Resources.FindObjectsOfTypeAll<Parameter>(); |
|||
foreach (var parameter in genericParameters) |
|||
{ |
|||
if (parameter.GetType() == parameterType) |
|||
parameters.Add(parameter); |
|||
} |
|||
} |
|||
parameters.Sort((p1, p2) => p1.parameterName.CompareTo(p2.parameterName)); |
|||
|
|||
return parameters.ToArray(); |
|||
} |
|||
|
|||
string[] GetStringOptions(Parameter[] parameters) |
|||
{ |
|||
var options = new string[parameters.Length + 1]; |
|||
options[0] = "None"; |
|||
for (var i = 1; i <= parameters.Length; i++) |
|||
{ |
|||
var parameter = parameters[i - 1]; |
|||
var metadata = ParameterMetaData.GetMetaData(parameter.GetType()); |
|||
options[i] = $"{parameter.parameterName} ({metadata.typeDisplayName})"; |
|||
} |
|||
return options; |
|||
} |
|||
|
|||
class ParameterDrawerBinding : IBinding |
|||
{ |
|||
ParameterDrawerElement m_Element; |
|||
|
|||
public ParameterDrawerBinding(ParameterDrawerElement element) => m_Element = element; |
|||
|
|||
public void PreUpdate() { } |
|||
|
|||
public void Update() |
|||
{ |
|||
m_Element.UpdateMenuOptions(); |
|||
m_Element.m_ParameterMenu.text = m_Element.GetSelectedOptionText(); |
|||
} |
|||
|
|||
public void Release() { } |
|||
} |
|||
} |
|||
} |
|||
*/ |
|
|||
fileFormatVersion: 2 |
|||
guid: 3fd3617d27b14531a75a7a0ef1e45452 |
|||
timeCreated: 1596417699 |
撰写
预览
正在加载...
取消
保存
Reference in new issue