Version: Unity 6.6 Alpha (6000.6)
LanguageEnglish
  • C#

UxmlCreateInstanceMethodAttribute

class in UnityEngine.UIElements

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

Declares a method to create instances in place of the default constructor.

Use this to provide custom instance creation logic, such as object pooling or dependency injection. The method must meet the following requirements: - Be a static method defined on the element type. - Have public or internal accessibility. - Accept no parameters. - Not be generic. - Return an instance of the element type (or null to fall back to the default constructor).

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(); } } }