Version: 2023.2
LanguageEnglish
  • C#

UxmlAttributeConverter<T0>

class in UnityEditor.UIElements

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

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>

Public Methods

FromString Provides a type converted from a string representation.
ToString Provides a string representation of a type.