# VisualElementComponentAttribute

> Declares a struct as a UI Toolkit component: a reusable piece of data and behavior that you can attach to any VisualElement.

## Definition

* **Type:** Class
* **Namespace:** [UnityEngine.UIElements](/engine/6000.7/script-reference/unityengine/uielements.md)
* **Assembly:** UnityEngine.UIElementsModule
* **Inherits from:** [Attribute](https://learn.microsoft.com/dotnet/api/system.attribute)

```csharp
[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 `partial struct` with this attribute and attach an instance to any element with [VisualElement.AddComponent](/engine/6000.7/script-reference/unityengine/uielements/visualelement/addcomponent.md). The same component type works on any element, whatever its type.

This attribute targets structs. The struct must be declared `partial`: Unity's source generator completes it, implementing [IVisualElementComponent](/engine/6000.7/script-reference/unityengine/uielements/ivisualelementcomponent.md) and generating the registration code for you.

An element holds at most one component of each type. Read or modify an attached component with [VisualElement.GetComponent](/engine/6000.7/script-reference/unityengine/uielements/visualelement/getcomponent.md); remove it with [VisualElement.RemoveComponent](/engine/6000.7/script-reference/unityengine/uielements/visualelement/removecomponent.md).

Add [HideInInspector](/engine/6000.7/script-reference/unityengine/hideininspector.md) 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 [\_exposeToUxml](/engine/6000.7/script-reference/unityengine/uielements/visualelementcomponentattribute/exposetouxml.md), which decides only UXML authoring.

The following example attaches a click counter to a button without subclassing it.

## Examples

```csharp
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](/engine/6000.7/script-reference/unityengine/uielements/visualelementcomponentattribute/initialcapacity.md) | 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](/engine/6000.7/script-reference/unityengine/uielements/visualelementcomponentattribute/name.md)                       | The name that identifies this component in UXML. If not set, the struct's name is used.                                                                               |

## Constructors

| Constructor                                                                                                                                       | Description                                                                                                                                                           |
| ------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [VisualElementComponentAttribute(System.Boolean)](/engine/6000.7/script-reference/unityengine/uielements/visualelementcomponentattribute/ctor.md) | Initializes a new instance of the [VisualElementComponentAttribute](/engine/6000.7/script-reference/unityengine/uielements/visualelementcomponentattribute.md) class. |

## Properties

| Property                                                                                                               | Description                                                                                                                                                                                                                                                                                                                                  |
| ---------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [exposeToUxml](/engine/6000.7/script-reference/unityengine/uielements/visualelementcomponentattribute/exposetouxml.md) | Whether the component can be authored in UXML. The fields marked [UxmlAttributeAttribute](/engine/6000.7/script-reference/unityengine/uielements/uxmlattributeattribute.md) define the component's authorable surface either way; this flag only decides whether UXML sees it. Pass `false` for a component that is attached from code only. |

## Inheritance

**Inherited Members:**

* [Attribute.GetCustomAttributes(MemberInfo, Type)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattributes#system-attribute-getcustomattributes\(system-reflection-memberinfo-system-type\))
* [Attribute.GetCustomAttributes(MemberInfo, Type, bool)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattributes#system-attribute-getcustomattributes\(system-reflection-memberinfo-system-type-system-boolean\))
* [Attribute.GetCustomAttributes(MemberInfo)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattributes#system-attribute-getcustomattributes\(system-reflection-memberinfo\))
* [Attribute.GetCustomAttributes(MemberInfo, bool)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattributes#system-attribute-getcustomattributes\(system-reflection-memberinfo-system-boolean\))
* [Attribute.IsDefined(MemberInfo, Type)](https://learn.microsoft.com/dotnet/api/system.attribute.isdefined#system-attribute-isdefined\(system-reflection-memberinfo-system-type\))
* [Attribute.IsDefined(MemberInfo, Type, bool)](https://learn.microsoft.com/dotnet/api/system.attribute.isdefined#system-attribute-isdefined\(system-reflection-memberinfo-system-type-system-boolean\))
* [Attribute.GetCustomAttribute(MemberInfo, Type)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattribute#system-attribute-getcustomattribute\(system-reflection-memberinfo-system-type\))
* [Attribute.GetCustomAttribute(MemberInfo, Type, bool)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattribute#system-attribute-getcustomattribute\(system-reflection-memberinfo-system-type-system-boolean\))
* [Attribute.GetCustomAttributes(ParameterInfo)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattributes#system-attribute-getcustomattributes\(system-reflection-parameterinfo\))
* [Attribute.GetCustomAttributes(ParameterInfo, Type)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattributes#system-attribute-getcustomattributes\(system-reflection-parameterinfo-system-type\))
* [Attribute.GetCustomAttributes(ParameterInfo, Type, bool)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattributes#system-attribute-getcustomattributes\(system-reflection-parameterinfo-system-type-system-boolean\))
* [Attribute.GetCustomAttributes(ParameterInfo, bool)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattributes#system-attribute-getcustomattributes\(system-reflection-parameterinfo-system-boolean\))
* [Attribute.IsDefined(ParameterInfo, Type)](https://learn.microsoft.com/dotnet/api/system.attribute.isdefined#system-attribute-isdefined\(system-reflection-parameterinfo-system-type\))
* [Attribute.IsDefined(ParameterInfo, Type, bool)](https://learn.microsoft.com/dotnet/api/system.attribute.isdefined#system-attribute-isdefined\(system-reflection-parameterinfo-system-type-system-boolean\))
* [Attribute.GetCustomAttribute(ParameterInfo, Type)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattribute#system-attribute-getcustomattribute\(system-reflection-parameterinfo-system-type\))
* [Attribute.GetCustomAttribute(ParameterInfo, Type, bool)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattribute#system-attribute-getcustomattribute\(system-reflection-parameterinfo-system-type-system-boolean\))
* [Attribute.GetCustomAttributes(Module, Type)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattributes#system-attribute-getcustomattributes\(system-reflection-module-system-type\))
* [Attribute.GetCustomAttributes(Module)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattributes#system-attribute-getcustomattributes\(system-reflection-module\))
* [Attribute.GetCustomAttributes(Module, bool)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattributes#system-attribute-getcustomattributes\(system-reflection-module-system-boolean\))
* [Attribute.GetCustomAttributes(Module, Type, bool)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattributes#system-attribute-getcustomattributes\(system-reflection-module-system-type-system-boolean\))
* [Attribute.IsDefined(Module, Type)](https://learn.microsoft.com/dotnet/api/system.attribute.isdefined#system-attribute-isdefined\(system-reflection-module-system-type\))
* [Attribute.IsDefined(Module, Type, bool)](https://learn.microsoft.com/dotnet/api/system.attribute.isdefined#system-attribute-isdefined\(system-reflection-module-system-type-system-boolean\))
* [Attribute.GetCustomAttribute(Module, Type)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattribute#system-attribute-getcustomattribute\(system-reflection-module-system-type\))
* [Attribute.GetCustomAttribute(Module, Type, bool)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattribute#system-attribute-getcustomattribute\(system-reflection-module-system-type-system-boolean\))
* [Attribute.GetCustomAttributes(Assembly, Type)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattributes#system-attribute-getcustomattributes\(system-reflection-assembly-system-type\))
* [Attribute.GetCustomAttributes(Assembly, Type, bool)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattributes#system-attribute-getcustomattributes\(system-reflection-assembly-system-type-system-boolean\))
* [Attribute.GetCustomAttributes(Assembly)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattributes#system-attribute-getcustomattributes\(system-reflection-assembly\))
* [Attribute.GetCustomAttributes(Assembly, bool)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattributes#system-attribute-getcustomattributes\(system-reflection-assembly-system-boolean\))
* [Attribute.IsDefined(Assembly, Type)](https://learn.microsoft.com/dotnet/api/system.attribute.isdefined#system-attribute-isdefined\(system-reflection-assembly-system-type\))
* [Attribute.IsDefined(Assembly, Type, bool)](https://learn.microsoft.com/dotnet/api/system.attribute.isdefined#system-attribute-isdefined\(system-reflection-assembly-system-type-system-boolean\))
* [Attribute.GetCustomAttribute(Assembly, Type)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattribute#system-attribute-getcustomattribute\(system-reflection-assembly-system-type\))
* [Attribute.GetCustomAttribute(Assembly, Type, bool)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattribute#system-attribute-getcustomattribute\(system-reflection-assembly-system-type-system-boolean\))
* [Attribute.Equals(Object)](https://learn.microsoft.com/dotnet/api/system.attribute.equals)
* [Attribute.GetHashCode()](https://learn.microsoft.com/dotnet/api/system.attribute.gethashcode)
* [Attribute.Match(Object)](https://learn.microsoft.com/dotnet/api/system.attribute.match)
* [Attribute.IsDefaultAttribute()](https://learn.microsoft.com/dotnet/api/system.attribute.isdefaultattribute)
* [Attribute.TypeId()](https://learn.microsoft.com/dotnet/api/system.attribute.typeid)
