Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


UxmlAttributeFieldDecorator

An Editor-only container for bindable fields. It adds an override indicator bar, binding state indicators, and a context menu for use in a custom drawer.
Read time 10 minutesLast updated 10 days ago

Definition

[UxmlElement]public class UxmlAttributeFieldDecorator : VisualElement, IEventHandler, IResolvedStyle, ITransform, ITransitionAnimations, IExperimentalFeatures, IVisualElementScheduler, ICustomStyle

Remarks

Use UxmlAttributeFieldDecorator to wrap a TextField or another BaseField<TValueType> in a custom layout or next to action buttons, while it still takes part in the override and binding system. For a plain property display, use UxmlAttributeField instead.
The decorator can hold several children, but it binds only its first direct IBindable child, and detects the binding path when that child binds to a SerializedProperty.
Right-click the decorator to open a context menu whose actions depend on the attribute state: add a binding, edit or remove a binding, unset the attribute, or unset all attributes on the element.
The following example wraps a text field in a decorator next to action buttons.

Examples

// Override CreateChildPropertyGUI to render specific properties with custom layouts.//// This example targets an InfoPanel element with two string [UxmlAttribute] properties:// - "text": a freeform description rendered as a multiline TextField with a Clear button.// - "documentationUrl": a URL rendered as a single-line TextField with an Open button.//// Both fields use a UxmlAttributeFieldDecorator so they retain override and binding affordances.// All other properties fall back to the default UxmlAttributeField rendering via the base class.[CustomPropertyDrawer(typeof(InfoPanel.UxmlSerializedData))]public class InfoPanelDrawer : UxmlSerializedDataPropertyDrawer{ protected override void CreateChildPropertyGUI(VisualElement container, SerializedProperty property, SerializedProperty childProperty) { switch (childProperty.name) { case "text": container.Add(BuildMultilineFieldWithClear(property, childProperty)); break; case "documentationUrl": container.Add(BuildUrlFieldWithOpen(property, childProperty)); break; default: base.CreateChildPropertyGUI(container, property, childProperty); break; } } // Builds a row with a multiline TextField inside a UxmlAttributeFieldDecorator and a Clear button. static VisualElement BuildMultilineFieldWithClear(SerializedProperty property, SerializedProperty childProperty) { // Cache the path and the object — both properties are references that may be recycled after // this method returns, so the button callback must not touch either one. string propertyPath = childProperty.propertyPath; SerializedObject serializedObject = property.serializedObject; VisualElement row = new VisualElement(); row.style.flexDirection = FlexDirection.Row; UxmlAttributeFieldDecorator decorator = new UxmlAttributeFieldDecorator(); TextField textField = new TextField(childProperty.displayName); textField.bindingPath = propertyPath; textField.multiline = true; textField.style.flexGrow = 1; decorator.Add(textField); row.Add(decorator); row.Add(new Button(() => { SerializedProperty serializedProperty = serializedObject.FindProperty(propertyPath); if (serializedProperty != null) { serializedProperty.stringValue = string.Empty; serializedObject.ApplyModifiedProperties(); } }) { text = "Clear" }); return row; } // Builds a row with a single-line TextField inside a UxmlAttributeFieldDecorator and an Open button. static VisualElement BuildUrlFieldWithOpen(SerializedProperty property, SerializedProperty childProperty) { string propertyPath = childProperty.propertyPath; SerializedObject serializedObject = property.serializedObject; VisualElement row = new VisualElement(); row.style.flexDirection = FlexDirection.Row; UxmlAttributeFieldDecorator decorator = new UxmlAttributeFieldDecorator(); TextField textField = new TextField(childProperty.displayName); textField.bindingPath = propertyPath; textField.style.flexGrow = 1; decorator.Add(textField); row.Add(decorator); row.Add(new Button(() => { SerializedProperty serializedProperty = serializedObject.FindProperty(propertyPath); if (serializedProperty != null && !string.IsNullOrEmpty(serializedProperty.stringValue)) Application.OpenURL(serializedProperty.stringValue); }) { text = "Open" }); return row; }}

Static Fields

Value

Description

ussClassNameThe USS class name for a UXML attribute field decorator.

Constructors

Constructor

Description

UxmlAttributeFieldDecorator()Creates a decorator that has no bound property.

Properties

Property

Description

contentContainerThe element that holds this decorator's children.

Inheritance

Inherited Members