VisualElementComponentAttribute
Declares a struct as a UI Toolkit component: a reusable piece of data and behavior that you can attach to any VisualElement.
Read time 7 minutesLast updated 4 days ago
Definition
- Type: Class
- Namespace: UnityEngine.UIElements
- Assembly: UnityEngine.UIElementsModule
- Inherits from: Attribute
[AttributeUsage(AttributeTargets.Struct, Inherited = false)]public class VisualElementComponentAttribute : Attribute
Remarks
Components extend elements through composition instead of inheritance. Rather than writing a custom element subclass, declare a with this attribute and attach an instance to any element with VisualElement.AddComponent. The same component type works on any element, whatever its type.
partial structThis attribute targets structs. The struct must be declared : Unity's source generator completes it, implementing IVisualElementComponent and generating the registration code for you.
partialAn element holds at most one component of each type. Read or modify an attached component with VisualElement.GetComponent; remove it with VisualElement.RemoveComponent.
Unity generates a setter for each field of a component that declares an [OnComponentChanged] handler. is the field name with its first letter capitalized. For example, a field named gets a setter.
SetXxx(ref T self, VisualElement owner, ...)XxxcountSetCountYou can write component data in the following ways:
-
The generatedsetter for the field. The setter writes the field, publishes the new value to data bindings, and schedules the [OnComponentChanged] handler to run once in the next update. If the new value equals the current one, the setter does nothing. Use the setter when it's available.
SetXxx -
A direct write through areference, such as the one VisualElement.GetComponent returns. A direct write changes the data in place, but doesn't publish to data bindings or schedule the handler.
ref T -
A data binding that targets a component field, for example. The binding writes the field and schedules the [OnComponentChanged] handler for you.
${component:Tooltip}.text
After a direct write, call the methods for the effects you need:
-
VisualElement.MarkComponentDirty schedules the [OnComponentChanged] handler to run once in the next update. It doesn't publish to data bindings.
-
VisualElement.NotifyComponentChanged publishes the change to data bindings. It doesn't run the handler.
To get the same result as the generated setter, call both methods.
Every hook a component declares receives the component data as a parameter: [OnComponentAdded], [OnComponentChanged], [OnComponentRemoved], [GenerateVisualContent], [RegisterCallback], and [ReleaseComponentResources]. The generated setters take the same parameter, and the following rules apply to them too.
ref T selfSetXxx(ref T self, ...)The parameter refers to the live component data on the element, so when a hook writes to , it changes the component directly. The parameter is temporary. It stays valid only until a component is added to or removed from the element.
selfselfselfA hook that receives the owner element can add and remove components on it. As soon as it does, directly or through code it calls, the parameter is no longer valid, and the hook must not read or write it again. If the hook still needs the component data, get it again with VisualElement.GetComponent. Don't store in a field. To keep access to a component, store the element and call VisualElement.GetComponent when you need the data.
selfselfAn [OnComponentChanged] handler can add or remove components while Unity runs the handlers for the element. Unity takes a snapshot of the element's components before it runs the handlers. If a handler removes a component, Unity skips that component's handler. If a handler adds a component, the new component's handler runs in the next update.
Add HideInInspector to the struct to keep the component out of every editor surface: the VisualElement inspector, the UI Builder inspector, and the UI Toolkit Debugger. This is independent of VisualElementComponentAttribute.exposeToUxml, which decides only UXML authoring.
The following example attaches a click counter to a button without subclassing it.
Examples
using UnityEngine;using UnityEngine.UIElements;[VisualElementComponent]public partial struct ClickCounter{ public int count;}public static class ClickCounterExample{ public static void Attach(Button button) { // Attach the component with its starting value. button.AddComponent(new ClickCounter { count = 0 }); button.RegisterCallback<ClickEvent>(evt => { // GetComponent returns a reference: the increment is stored on the element. ref var counter = ref button.GetComponent<ClickCounter>(); counter.count++; Debug.Log($"Clicked {counter.count} times."); }); }}
Fields
Value | Description |
|---|---|
| initialCapacity | The number of storage slots Unity reserves up front for this component type. Set it to roughly how many elements you expect to carry this component at the same time. |
| name | The name that identifies this component in UXML. If not set, the struct's name is used. |
Constructors
Constructor | Description |
|---|---|
| VisualElementComponentAttribute(System.Boolean) | Initializes a new instance of the VisualElementComponentAttribute class. |
Properties
Property | Description |
|---|---|
| exposeToUxml | Whether the component can be authored in UXML. The fields marked UxmlAttributeAttribute define the component's authorable surface either way; this flag only decides whether UXML sees it. Pass |