sleal-unity
4 年前
当前提交
389eca52
共有 12 个文件被更改,包括 320 次插入 和 4 次删除
-
2com.unity.perception/CHANGELOG.md
-
32com.unity.perception/Editor/Randomization/Editors/RunInUnitySimulationWindow.cs
-
1com.unity.perception/Editor/Randomization/Editors/ScenarioBaseEditor.cs
-
8com.unity.perception/Editor/Randomization/Uxml/RunInUnitySimulationWindow.uxml
-
52com.unity.perception/Editor/Randomization/PropertyDrawers/UIntDrawer.cs
-
11com.unity.perception/Editor/Randomization/PropertyDrawers/UIntDrawer.cs.meta
-
8com.unity.perception/Editor/Randomization/VisualElements/Basic.meta
-
136com.unity.perception/Editor/Randomization/VisualElements/Basic/UIntField.cs
-
11com.unity.perception/Editor/Randomization/VisualElements/Basic/UIntField.cs.meta
-
52com.unity.perception/Editor/Randomization/VisualElements/Basic/UxmlUIntAttributeDescription.cs
-
11com.unity.perception/Editor/Randomization/VisualElements/Basic/UxmlUIntAttributeDescription.cs.meta
|
|||
using System; |
|||
using UnityEngine; |
|||
using UnityEngine.UIElements; |
|||
using UnityEditor.UIElements; |
|||
|
|||
namespace UnityEditor.Perception.Randomization.PropertyDrawers |
|||
{ |
|||
[CustomPropertyDrawer(typeof(uint))] |
|||
class UIntDrawer : PropertyDrawer |
|||
{ |
|||
public override VisualElement CreatePropertyGUI(SerializedProperty property) |
|||
{ |
|||
var field = new UIntField |
|||
{ |
|||
label = property.displayName, |
|||
bindingPath = property.propertyPath, |
|||
value = (uint)property.longValue |
|||
}; |
|||
|
|||
field.RegisterValueChangedCallback(evt => |
|||
{ |
|||
field.value = evt.newValue; |
|||
property.longValue = evt.newValue; |
|||
property.serializedObject.ApplyModifiedProperties(); |
|||
}); |
|||
|
|||
// Create a surrogate integer field to detect and pass along external change events on the UIntField.
|
|||
// UIElements currently does not support default change detection on non-SerializedProperty supported types.
|
|||
var surrogateField = new IntegerField(); |
|||
field.Add(surrogateField); |
|||
surrogateField.style.display = new StyleEnum<DisplayStyle>(DisplayStyle.None); |
|||
surrogateField.bindingPath = property.propertyPath; |
|||
surrogateField.RegisterValueChangedCallback(evt => |
|||
{ |
|||
evt.StopImmediatePropagation(); |
|||
field.value = UIntField.ClampInput(property.longValue); |
|||
}); |
|||
|
|||
return field; |
|||
} |
|||
|
|||
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) |
|||
{ |
|||
EditorGUI.PropertyField(position, property, label, true); |
|||
} |
|||
|
|||
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) |
|||
{ |
|||
return EditorGUI.GetPropertyHeight(property); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: e363a3910d78fe943b8f50b68a2d2fb9 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
fileFormatVersion: 2 |
|||
guid: 93e88a2d53dd88f42ae287310c2a6ca3 |
|||
folderAsset: yes |
|||
DefaultImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
using System.Globalization; |
|||
using UnityEngine; |
|||
using UnityEngine.UIElements; |
|||
|
|||
namespace UnityEditor.UIElements |
|||
{ |
|||
/// <summary>
|
|||
/// <para>Makes a text field for entering an unsigned integer.</para>
|
|||
/// </summary>
|
|||
public class UIntField : TextValueField<uint> |
|||
{ |
|||
/// <summary>
|
|||
/// <para>USS class name of elements of this type.</para>
|
|||
/// </summary>
|
|||
public new static readonly string ussClassName = "unity-uint-field"; |
|||
/// <summary>
|
|||
/// <para>USS class name of labels in elements of this type.</para>
|
|||
/// </summary>
|
|||
public new static readonly string labelUssClassName = ussClassName + "__label"; |
|||
/// <summary>
|
|||
/// <para>USS class name of input elements in elements of this type.</para>
|
|||
/// </summary>
|
|||
public new static readonly string inputUssClassName = ussClassName + "__input"; |
|||
|
|||
/// <summary>
|
|||
/// <para>Constructor.</para>
|
|||
/// </summary>
|
|||
public UIntField() |
|||
: this(null) { } |
|||
|
|||
/// <summary>
|
|||
/// <para>Constructor.</para>
|
|||
/// </summary>
|
|||
/// <param name="maxLength">Maximum number of characters the field can take.</param>
|
|||
public UIntField(int maxLength) |
|||
: this(null, maxLength) { } |
|||
|
|||
public UIntField(string label, int maxLength = -1) |
|||
: base(label, maxLength, new UIntInput()) |
|||
{ |
|||
AddToClassList(ussClassName); |
|||
labelElement.AddToClassList(labelUssClassName); |
|||
labelElement.AddToClassList("unity-property-field__label"); |
|||
AddLabelDragger<uint>(); |
|||
} |
|||
|
|||
UIntInput uIntInput => (UIntInput)textInputBase; |
|||
|
|||
/// <summary>
|
|||
/// <para>Converts the given uint to a string.</para>
|
|||
/// </summary>
|
|||
/// <param name="v">The uint to be converted to string.</param>
|
|||
/// <returns>
|
|||
/// <para>The uint as string.</para>
|
|||
/// </returns>
|
|||
protected override string ValueToString(uint v) |
|||
{ |
|||
return v.ToString(formatString, CultureInfo.InvariantCulture.NumberFormat); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// <para>Converts a string to an uint.</para>
|
|||
/// </summary>
|
|||
/// <param name="str">The string to convert.</param>
|
|||
/// <returns>
|
|||
/// <para>The uint parsed from the string.</para>
|
|||
/// </returns>
|
|||
protected override uint StringToValue(string str) |
|||
{ |
|||
long.TryParse(str, out var result); |
|||
return ClampInput(result); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// <para>Modify the value using a 3D delta and a speed, typically coming from an input device.</para>
|
|||
/// </summary>
|
|||
/// <param name="delta">A vector used to compute the value change.</param>
|
|||
/// <param name="speed">A multiplier for the value change.</param>
|
|||
/// <param name="startValue">The start value.</param>
|
|||
public override void ApplyInputDeviceDelta(Vector3 delta, DeltaSpeed speed, uint startValue) |
|||
{ |
|||
uIntInput.ApplyInputDeviceDelta(delta, speed, startValue); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// <para>Instantiates an UIntField using the data read from a UXML file.</para>
|
|||
/// </summary>
|
|||
public new class UxmlFactory : UxmlFactory<UIntField, UxmlTraits> { } |
|||
|
|||
/// <summary>
|
|||
/// <para>Defines UxmlTraits for the UIntField.</para>
|
|||
/// </summary>
|
|||
public new class UxmlTraits : TextValueFieldTraits<uint, UxmlUIntAttributeDescription> { } |
|||
|
|||
public static uint ClampInput(long input) |
|||
{ |
|||
input = input > uint.MaxValue ? uint.MaxValue : input; |
|||
input = input < uint.MinValue ? uint.MinValue : input; |
|||
return (uint)input; |
|||
} |
|||
|
|||
class UIntInput : TextValueInput |
|||
{ |
|||
internal UIntInput() |
|||
{ |
|||
formatString = "#######0"; |
|||
} |
|||
|
|||
UIntField parentUIntField => (UIntField)parent; |
|||
|
|||
protected override string allowedCharacters => "0123456789"; |
|||
|
|||
public override void ApplyInputDeviceDelta(Vector3 delta, DeltaSpeed speed, uint startValue) |
|||
{ |
|||
var num = StringToValue(text) + (long)Math.Round(delta.x); |
|||
var value = ClampInput(num); |
|||
if (parentUIntField.isDelayed) |
|||
text = ValueToString(value); |
|||
else |
|||
parentUIntField.value = value; |
|||
} |
|||
|
|||
protected override string ValueToString(uint v) |
|||
{ |
|||
return v.ToString(formatString); |
|||
} |
|||
|
|||
protected override uint StringToValue(string str) |
|||
{ |
|||
long.TryParse(str, out var result); |
|||
return ClampInput(result); |
|||
} |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 64ee321fd1c586b4a9dbfc0f7da230be |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
using System.Globalization; |
|||
|
|||
namespace UnityEngine.UIElements |
|||
{ |
|||
/// <summary>
|
|||
/// <para>Describes a XML int attribute.</para>
|
|||
/// </summary>
|
|||
public class UxmlUIntAttributeDescription : TypedUxmlAttributeDescription<uint> |
|||
{ |
|||
/// <summary>
|
|||
/// <para>Constructor.</para>
|
|||
/// </summary>
|
|||
public UxmlUIntAttributeDescription() |
|||
{ |
|||
type = "int"; |
|||
typeNamespace = "http://www.w3.org/2001/XMLSchema"; |
|||
defaultValue = 0; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// <para>The default value for the attribute, as a string.</para>
|
|||
/// </summary>
|
|||
public override string defaultValueAsString => defaultValue.ToString(CultureInfo.InvariantCulture.NumberFormat); |
|||
|
|||
/// <summary>
|
|||
/// <para>
|
|||
/// Retrieves the value of this attribute from the attribute bag. Returns it if it is found, otherwise return
|
|||
/// defaultValue.
|
|||
/// </para>
|
|||
/// </summary>
|
|||
/// <param name="bag">The bag of attributes.</param>
|
|||
/// <param name="cc">The context in which the values are retrieved.</param>
|
|||
/// <returns>
|
|||
/// <para>The value of the attribute.</para>
|
|||
/// </returns>
|
|||
public override uint GetValueFromBag(IUxmlAttributes bag, CreationContext cc) |
|||
{ |
|||
return GetValueFromBag(bag, cc, (s, i) => ConvertValueToUInt(s, i), defaultValue); |
|||
} |
|||
|
|||
public bool TryGetValueFromBag(IUxmlAttributes bag, CreationContext cc, ref uint value) |
|||
{ |
|||
return TryGetValueFromBag(bag, cc, (s, i) => ConvertValueToUInt(s, i), defaultValue, ref value); |
|||
} |
|||
|
|||
static uint ConvertValueToUInt(string v, uint defaultValue) |
|||
{ |
|||
return v == null || !uint.TryParse(v, out uint result) ? defaultValue : result; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 2e94e3964dd6654488e74ca218f23111 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
撰写
预览
正在加载...
取消
保存
Reference in new issue