Documentation

Unity Engine

Download Unity

Unity Engine

GetOrAddComponent<T>()

Returns a reference to the component of type T attached to this element. If the component is not attached yet, it is added first.
Read time 1 minuteLast updated 10 hours ago

Definition

public ref T GetOrAddComponent<T>() where T : struct, IVisualElementComponent

Returns

Type

Description

TA reference to the live component data. The reference is temporary, so don't store it in a field. For details, refer to VisualElement.GetComponent.

Remarks

When the component is already attached, this method behaves exactly like VisualElement.GetComponent and the component data is not changed. When it is not attached, a new component is created and added the same way as VisualElement.AddComponent. The new component is created with the type's UxmlCreateInstanceMethodAttribute method when the type has one, otherwise with default values.