RequiresComponentOfTypeAttribute
Declares that a component needs another component type on the same element. When the declaring component is added to an element, from code or from UXML, the required components are added first, in declaration order. A required component that is already attached keeps its values. A required component that is missing is added with its default values.
Read time 4 minutesLast updated 10 hours ago
Definition
- Type: Class
- Namespace: UnityEngine.UIElements
- Assembly: UnityEngine.UIElementsModule
- Inherits from: Attribute
[AttributeUsage(AttributeTargets.Struct, AllowMultiple = true, Inherited = false)]public sealed class RequiresComponentOfTypeAttribute : Attribute
Remarks
The required type must be a struct decorated with VisualElementComponentAttribute. Requirements must not form a cycle: a component cannot require itself, directly or through other components. Both cases are compile-time errors.
This attribute affects only how the component is added. Unity adds the required components when you add the declaring component, but doesn't keep them attached afterward. You can remove a component that another attached component still requires. In the Unity Editor, Unity logs a warning. In Player builds, Unity doesn't check. Before a component reads a component it requires, make sure that component is still attached, for example with VisualElement.HasComponent.
Constructors
Constructor | Description |
|---|---|
| RequiresComponentOfTypeAttribute(System.Type) | Initializes the attribute with the required component type. |
Properties
Property | Description |
|---|---|
| ComponentType | The component type that must exist on the same element. |