Version: 2023.2
Bind custom controls to data
Best practices for managing elements

Define a namespace prefix

After you have defined a custom control element, you can use it in your UXML files. To categorize elements, create the class in a namespace. When you define a new namespace, you can define a prefix for the namespace. You must define namespace prefixes as attributes of the root <UXML> element and replace the full namespace name when scoping elements.

To define a namespace prefix, add a UxmlNamespacePrefix attribute to your assembly for each namespace prefix. For example:

[assembly: UxmlNamespacePrefix("My.First.Namespace", "first")]
[assembly: UxmlNamespacePrefix("My.Second.Namespace", "second")]

此操作可以在程序集的任何 C# 文件的根级别(在任何命名空间之外)完成。

架构生成系统会执行以下操作:

  • 检查是否有这些属性并使用它们来生成架构。
  • Adds the namespace prefix definition as an attribute of the <UXML> element in newly created UXML files.
  • 将命名空间的架构文件位置包含在其 xsi:schemaLocation 属性中。

To ensure that your text editor recognizes the new element, select Assets > Update UXML Schema to update the schema definition.

To create a new UXML document with the prefix, select Assets > Create > UI Toolkit > UI Document.

其他资源

Bind custom controls to data
Best practices for managing elements