class in UnityEngine.UIElements
/
Implemented in:UnityEngine.UIElementsModule
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.
CloseFor 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.
CloseDeclares that a field or property is associated with a UXML attribute.
You can use the UxmlAttribute attribute to declare that a property or field is associated with a UXML attribute.
When an element is marked with the UxmlElement attribute, a corresponding UxmlSerializedData class is
generated in the partial class.
This data class contains a SerializeField for each field or property that's marked with the UxmlAttribute attribute.
When a field or property is associated with a UXML attribute, all of its attributes are transferred over to the serialized version.
This allows for the support of custom property drawers and decorator attributes, such as HeaderAttribute, TextAreaAttribute,
RangeAttribute, TooltipAttribute, and so on.
The following example creates a custom control with custom attributes:
using UnityEngine.UIElements;
namespace Example { [UxmlElement] public partial class MyElement : VisualElement { [UxmlAttribute] public string myStringValue { get; set; }
[UxmlAttribute] public float myFloatValue { get; set; }
[UxmlAttribute] public int myIntValue { get; set; }
[UxmlAttribute] public UsageHints myEnumValue { get; set; } } }
Unity objects can also be UxmlAttributes and will contain a reference to the asset file when serialized as UXML. The following example creates a custom control with custom attributes. The types of attributes are UXML template and texture:
using UnityEngine; using UnityEngine.UIElements;
[UxmlElement] public partial class CustomElement : VisualElement { [UxmlAttribute] public VisualTreeAsset myTemplate { get; set; }
[UxmlAttribute] public Texture2D Texture2D { get; set; } }
By default, when resolving the attribute name, the field or property name splits into lowercase words connected by hyphens.
The original uppercase characters in the name are used to denote where the name should be split. For example, if the property name
is myIntValue
, the corresponding attribute name would be my-int-value
.
You can customize the attribute name through the UxmlAttribute name argument.
The following example creates a custom control with customized attribute names:
using UnityEngine.UIElements;
[UxmlElement] public partial class CustomAttributeNameExample : VisualElement { [UxmlAttribute("character-name")] public string myStringValue { get; set; }
[UxmlAttribute("credits")] public float myFloatValue { get; set; }
[UxmlAttribute("level")] public int myIntValue { get; set; }
[UxmlAttribute("usage")] public UsageHints myEnumValue { get; set; } }
Example UXML:
<ui:UXML xmlns:ui="UnityEngine.UIElements"> <CustomAttributeNameExample character-name="Karl" credits="1.23" level="1" usage="DynamicColor" /> </ui:UXML>
If you've changed the name of an attribute and want to ensure that UXML files with the previous attribute name can still
be loaded by UI Builder, use the obsoleteNames
argument.
This argument matches attributes in the UXML to be applied to the attribute during serialization.
UI Builder uses the new name when loading the UXML file.
The following example creates a custom control with custom attributes that uses obsolete attribute names:
using UnityEngine.UIElements;
[UxmlElement] public partial class CharacterDetails : VisualElement { [UxmlAttribute("character-name", "npc-name")] public string npcName { get; set; }
[UxmlAttribute("character-health", "health")] public float health { get; set; } }
The following example UXML uses the obsolete names:
<ui:UXML xmlns:ui="UnityEngine.UIElements"> <CharacterDetails npc-name="Haydee" health="100" /> </ui:UXML>
When you create each SerializeField, all attributes are copied across. This allows you to use decorators and custom property drawers on fields in the UI Builder. The following example uses a custom control with decorators on its attribute fields:
using UnityEngine.UIElements; using UnityEngine;
[UxmlElement] public partial class ExampleText : VisualElement { [TextArea, UxmlAttribute] public string myText;
[Header("My Header")] [Range(0, 100)] [UxmlAttribute] public int rangedInt;
[Tooltip("My custom tooltip")] [Min(10)] [UxmlAttribute] public int minValue = 100; }
The UI Builder displays the attributes with the decorators:
The following example creates a custom control with a custom property drawer:
using UnityEngine; using UnityEngine.UIElements;
public class MyDrawerAttribute : PropertyAttribute { }
[UxmlElement] public partial class MyDrawerExample : Button { [UxmlAttribute] public Color myColor;
[MyDrawer, UxmlAttribute] public string myText; }
using UnityEditor; using UnityEditor.UIElements; using UnityEngine.UIElements;
[CustomPropertyDrawer(typeof(MyDrawerAttribute))] public class MyDrawerAttributePropertyDrawer : PropertyDrawer { public override VisualElement CreatePropertyGUI(SerializedProperty property) { var row = new VisualElement { style = { flexDirection = FlexDirection.Row } }; var textField = new TextField("My Text") { style = { flexGrow = 1 } }; var button = new Button { text = ":" }; button.clicked += () => textField.value = "RESET";
// Get the parent property var parentPropertyPath = property.propertyPath.Substring(0, property.propertyPath.LastIndexOf('.')); var parent = property.serializedObject.FindProperty(parentPropertyPath);
var colorProp = parent.FindPropertyRelative("myColor"); textField.TrackPropertyValue(colorProp, p => { row.style.backgroundColor = p.colorValue; });
row.style.backgroundColor = colorProp.colorValue; row.Add(textField); row.Add(button); textField.BindProperty(property);
return row; } }
You can use struct or class instances as attributes and even lists of struct or class instances in UXML.
However, they must be convertible to and from a string and you must declare a UxmlAttributeConverter<T0> to support this conversion.
When using the class in a list, ensure that its string representation does not contain any commas (",") as this character is used by
the list to separate the items. Refer to UxmlObjectReferenceAttribute for an example of a field that supports more complex data.
The following example shows how a class instance can support a property and list:
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}"); }
<ui:UXML xmlns:ui="UnityEngine.UIElements"> <MyElementWithData some-data="1|2.3" lots-of-data="1|2,3|4,5|6" /> </ui:UXML>
When an attribute shares the same UXML name as an attribute from a child class, it takes precedence and overrides that field, appearing in the inspector instead. With this feature, you can substitute a field with a different type or a custom property. The following example demonstrates how to replace the IntegerField value with a SliderField.
using UnityEngine.UIElements; using UnityEngine;
public class MyOverrideDrawerAttribute : PropertyAttribute { }
[UxmlElement] public partial class IntFieldWithCustomPropertyDrawer : IntegerField { // Override the value property to use the custom drawer [UxmlAttribute("value"), MyOverrideDrawer] internal int myOverrideValue { get => this.value; set => this.value = value; } }
using UnityEditor; using UnityEditor.UIElements; using UnityEngine.UIElements;
[CustomPropertyDrawer(typeof(MyOverrideDrawerAttribute))] public class MyOverrideDrawerAttributePropertyDrawer : PropertyDrawer { public override VisualElement CreatePropertyGUI(SerializedProperty property) { var field = new SliderInt(0, 100) { label = "Value" }; field.BindProperty(property); return field; } }
name | Provides a custom UXML name to the attribute. |
obsoleteNames | Provides support for obsolete UXML attribute names. |
UxmlAttributeAttribute | Declares that a field or property is associated with a UXML attribute. |
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.
When you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.
More information
These cookies enable the website to provide enhanced functionality and personalisation. They may be set by us or by third party providers whose services we have added to our pages. If you do not allow these cookies then some or all of these services may not function properly.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
These cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising. Some 3rd party video providers do not allow video views without targeting cookies. If you are experiencing difficulty viewing a video, you will need to set your cookie preferences for targeting to yes if you wish to view videos from these providers. Unity does not control this.
These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.