Declares a method to create instances in place of the default constructor.
Use this to provide custom instance creation logic, such as object pooling, dependency
injection, or default values for a component. Three callers use the method: UXML
instantiation (element and component defaults), GetOrAddComponent when it creates a
missing component, and a RequiresComponentOfTypeAttribute auto-add.
The method must meet the following requirements:
If this attribute is not found on the element, the source generator uses the default constructor (new T()).
If the method returns null, the source generator uses the default constructor as a fallback.
The following example uses a custom creation method for object pooling:
using UnityEngine.UIElements; using UnityEngine.Pool;
namespace MyNamespace { [UxmlElement] public partial class MyButtonElement : Button { [UxmlCreateInstanceMethodAttribute] public static MyButtonElement CreateInstance() { return GenericPool<MyButtonElement>.Get(); } } }