Converts a UxmlAttribute type to and from a string.
Fields marked with UxmlAttributeAttribute are represented in UXML by a single string attribute,
however, to properly serialize these attributes, you must declare a UxmlAttributeConverter.
This converter converts the string attribute value into the appropriate data type for the marked field.
Note: The following types have native support and you can use them without declaring a UxmlAttributeConverter:
The following example creates a custom control that uses a class instance and a list of class instances as its attributes.
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; }
To support the class, declare a converter:
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]) }; }
public override string ToString(MyClassWithData value) => $"{value.myInt}|{value.myFloat}"; }
Example UXML:
<ui:UXML xmlns:ui="UnityEngine.UIElements"> <MyElementWithData some-data="1|2.3" lots-of-data="1|2,3|4,5|6" /> </ui:UXML>
FromString | Provides a type converted from a string representation. |
ToString | Provides a string representation of a type. |