将 UxmlAttribute 类型转换为字符串,反之亦然。
使用 UxmlAttributeAttribute 标记的字段在 UXML 中由单个字符串属性表示,但是,要正确序列化这些属性,必须声明一个 UxmlAttributeConverter。此转换器将字符串属性值转换为标记字段的适当数据类型。
注意:以下类型具有原生支持,您可以使用它们而无需声明 UxmlAttributeConverter
以下示例创建了一个自定义控件,该控件使用类实例和类实例列表作为其属性。
using System; using System.Collections.Generic; using UnityEngine.UIElements;
[Serializable] public class MyClassWithData { public int myInt; public float myFloat; }
[UxmlElement] public partial class MyElementWithData : VisualElement { [UxmlAttribute] public MyClassWithData someData;
[UxmlAttribute] public List<MyClassWithData> lotsOfData; }
要支持该类,请声明一个转换器
using System; using System.Globalization; using UnityEditor.UIElements;
public class MyClassWithDataConverter : UxmlAttributeConverter<MyClassWithData> { public override MyClassWithData FromString(string value) { // Split using a | so that comma (,) can be used by the list. var split = value.Split('|');
return new MyClassWithData { myInt = int.Parse(split[0]), myFloat = float.Parse(split[1], CultureInfo.InvariantCulture) }; }
public override string ToString(MyClassWithData value) => FormattableString.Invariant($"{value.myInt}|{value.myFloat}"); }
UXML 示例
<ui:UXML xmlns:ui="UnityEngine.UIElements"> <MyElementWithData some-data="1|2.3" lots-of-data="1|2,3|4,5|6" /> </ui:UXML>
您还可以使用泛型属性转换器。但是,必须使用泛型类型声明属性。要使用派生自泛型类型的类型,请专门为此类型声明一个新的转换器。以下示例使用泛型属性转换器和自定义属性绘制器来创建泛型序列化字典
using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.UIElements;
[Serializable] public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, ISerializationCallbackReceiver { [SerializeField] public List<TKey> keys = new List<TKey>(); [SerializeField] public List<TValue> values = new List<TValue>();
void ISerializationCallbackReceiver.OnAfterDeserialize() => TransferSerializedKeys(); void ISerializationCallbackReceiver.OnBeforeSerialize() { }
public void TransferSerializedKeys() { Clear();
for (var i = 0; i < Math.Min(keys.Count, values.Count); i++) { this[keys[i]] = values[i]; } } }
[UxmlElement] public partial class MyDictElement : VisualElement { [UxmlAttribute] public SerializableDictionary<int, string> dictionaryIntString;
[UxmlAttribute] public SerializableDictionary<int, int> dictionaryIntInt;
[UxmlAttribute] public SerializableDictionary<string, string> dictionaryStringString; }
泛型属性转换器
using System; using System.Globalization; using System.Text; using UnityEditor.UIElements;
public class SerializableDictionaryConverter<TKey, TValue> : UxmlAttributeConverter<SerializableDictionary<TKey, TValue>> { static string ValueToString(object v) => Convert.ToString(v, CultureInfo.InvariantCulture);
public override string ToString(SerializableDictionary<TKey, TValue> source) { var sb = new StringBuilder(); foreach (var pair in source) { sb.Append($"{ValueToString(pair.Key)}|{ValueToString(pair.Value)},"); } return sb.ToString(); }
public override SerializableDictionary<TKey, TValue> FromString(string source) { var result = new SerializableDictionary<TKey, TValue>(); var items = source.Split(','); foreach (var item in items) { var fields = item.Split('|'); var key = (TKey)Convert.ChangeType(fields[0], typeof(TKey)); var value = (TValue)Convert.ChangeType(fields[1], typeof(TValue));
result.keys.Add(key); result.values.Add(value); } result.TransferSerializedKeys(); return result; } }
自定义属性绘制器
using UnityEditor; using UnityEditor.UIElements; using UnityEngine.UIElements;
[CustomPropertyDrawer(typeof(SerializableDictionary<,>))] class SerializableDictionaryPropertyDrawer : PropertyDrawer { SerializedProperty m_Property; SerializedProperty m_Keys; SerializedProperty m_Values;
public override VisualElement CreatePropertyGUI(SerializedProperty property) { m_Property = property; m_Keys = property.FindPropertyRelative("keys"); m_Values = property.FindPropertyRelative("values");
var list = new ListView() { showAddRemoveFooter = true, showBorder = true, showAlternatingRowBackgrounds = AlternatingRowBackground.All, showFoldoutHeader = true, showBoundCollectionSize = false, reorderable = true, reorderMode = ListViewReorderMode.Animated, virtualizationMethod = CollectionVirtualizationMethod.DynamicHeight, headerTitle = property.displayName, bindingPath = m_Keys.propertyPath, overridingAddButtonBehavior = OnAddButton, bindItem = BindListItem, }; return list; }
void BindListItem(VisualElement item, int index) { item.Clear();
item.Add(new PropertyField(m_Keys.GetArrayElementAtIndex(index)) { label = "Key" }); item.Add(new PropertyField(m_Values.GetArrayElementAtIndex(index)) { label = "Value" }); item.Bind(m_Property.serializedObject); }
void OnAddButton(BaseListView baseListView, Button button) { m_Keys.InsertArrayElementAtIndex(m_Keys.arraySize); m_Values.InsertArrayElementAtIndex(m_Values.arraySize); m_Property.serializedObject.ApplyModifiedProperties(); } }
FromString | 提供从字符串表示形式转换的类型。 |
ToString | 提供类型的字符串表示形式。 |