UxmlTemplateAttribute

Attribute that binds a UXML template to a class by name. Provides lazy-loaded access to the VisualTreeAsset via GetVisualTreeAsset(), which searches the AssetDatabase on first access and caches the result.

Definition

Namespace: Paragon.Editor Assembly: Paragon.Editor.dll

public class UxmlTemplateAttribute : Attribute

Inheritance: AttributeUxmlTemplateAttribute

Remarks

UxmlTemplateAttribute provides a declarative way to associate a UXML template with an editor class (e.g., a custom editor, property drawer, or editor window). Instead of manually loading the VisualTreeAsset in each class, the attribute handles the lookup and caching.

Template Resolution

The attribute finds the UXML file by name using AssetDatabase.FindAssets() with a t:VisualTreeAsset type filter. It then matches by file name (without extension) to ensure the correct asset is returned, even if multiple VisualTreeAsset results exist.

Caching

The VisualTreeAsset is loaded on the first call to GetVisualTreeAsset() and cached for subsequent calls. This avoids repeated AssetDatabase searches.

Error Handling

If no VisualTreeAsset with the specified name is found, GetVisualTreeAsset() throws a FileNotFoundException with a descriptive message.

Quick Lookup

Goal
How

Bind a UXML template

[UxmlTemplate("MyTemplate")] on the class

Load the template

GetType().GetCustomAttribute<UxmlTemplateAttribute>().GetVisualTreeAsset()

Clone into element

asset.CloneTree(rootElement)

Template location

Anywhere in the project — found by name via AssetDatabase

Fields

Field
Type
Access
Description

name

string

private readonly

The name of the UXML template to search for (without extension)

visualTreeAsset

VisualTreeAsset

private

Cached VisualTreeAsset (null until first access)

Constructors

UxmlTemplateAttribute(string)

Creates the attribute with the specified template name.

Parameter
Type
Description

name

string

Name of the UXML template file (without path or extension)

Methods

GetVisualTreeAsset

Loads and returns the VisualTreeAsset matching the template name. Caches the result after the first successful lookup.

Returns: The VisualTreeAsset for the named template.

Throws: FileNotFoundException — if no VisualTreeAsset with the specified name exists in the AssetDatabase.

Algorithm:

  1. If visualTreeAsset is already cached, returns it immediately

  2. Searches AssetDatabase with FindAssets("t:VisualTreeAsset {name}")

  3. Converts GUIDs to asset paths

  4. Finds the first path whose file name (without extension) matches name exactly

  5. Loads the asset and caches it

  6. If no match is found, throws FileNotFoundException

Common Pitfalls

circle-exclamation
circle-exclamation
circle-exclamation
circle-info

Template can be anywhere in the project The AssetDatabase search is project-wide. The UXML file does not need to be in the same folder as the class — it just needs to be somewhere under Assets/.

Examples

Decorating an Editor

Using in an EditorWindow

See Also

Last updated