Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


Load UXML and USS in C# scripts

Load UXML and USS as you would load a regular Unity asset.
Read time 3 minutesLast updated 25 days ago

Unity represents UXML files as
VisualTreeAsset
and USS files as
StyleSheet
objects in C#. Since both are standard Unity assets, you can use Unity's standard workflows to load them.

Use serialization references

Unity automatically detects fields from your C# scripts that are of type
VisualTreeAsset
or
StyleSheet
. You can use the Inspector to set references to specific UXML or USS files imported into your project. Such references remain valid even when the location of your assets changes in your project.
There are three ways to use this in your scripts:

Description

How to display the Inspector

Reference save location

Instances of custom scripts (such as
MonoBehaviour
)
Select the GameObject holding the instance of the scriptInside the scene
Default references for a script that derives from
EditorWindow
or
Editor
Select the actual C# file in the Project BrowserInside the meta file associated with the script
Custom assets in your project that derive from
ScriptableObject
Select the asset in the Project browserInside the serialized data of the asset itself
Note
The default references work for all scripts that derive from
MonoBehaviour
or
ScriptableObject
. It provides a way to populate default values for serialized fields of scripts.
The following example
MonoBehaviour
class receives a UXML file and a list of USS files from the Inspector:
using UnityEngine;using UnityEngine.UIElements;public class MyBehaviour : MonoBehaviour{ // Note that public fields are automatically exposed in the Inspector. public VisualTreeAsset exampleUI; public StyleSheet[] exampleStyle;}

The Inspector window shows a single reference picker and a list of style sheets (the list is empty)

The following example
EditorWindow
class receives default references from the Inspector:
using UnityEditor;using UnityEngine.UIElements;public class MyWindow : EditorWindow{ [SerializeField] private VisualTreeAsset uxml; [SerializeField] private StyleSheet uss;}

The Inspector window shows three reference fields: View Data Dictionary, UXML, and USS

Use the Asset Database (Editor only)

You can load your UI Assets by path or GUID with the
AssetDatabase
class.
The following example shows how to locate an asset by path:
VisualTreeAsset uxml = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/Editor/main_window.uxml");StyleSheet uss = AssetDatabase.LoadAssetAtPath<StyleSheet>("Assets/Editor/main_styles.uss");
The following example shows how to locate an asset by path from a package:
VisualTreeAsset uxml = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Packages/<name-of-the-package>/main_window.uxml");StyleSheet uss = AssetDatabase.LoadAssetAtPath<StyleSheet>("Packages/<name-of-the-package>/main_styles.uss");

Use Addressables

The Addressables system provides tools and scripts to organize and package content, and an API to load and release assets at runtime.
You can use UXML and USS assets with the Addressable system. A code-less way to use addressables is to create a prefab out of the UI Document component and then add that prefab as an addressable.
You can also use the Addressable API to load UXML and USS assets directly. Refer to Load UI Assets with Addressables for an example.

Use a Resources folder

If you add a
Resources
folder in your project and place your UI assets in it, you can use the
Resources.Load
method to load your assets.
The following examples show how to load an asset in the
Resources
folder:
VisualTreeAsset uxml = Resources.Load<VisualTreeAsset>("main_window");StyleSheet uss = Resources.Load<StyleSheet>("main_styles");
Note
This method increases the final build size and memory loading time significantly because all files in the
Resources
folder are included in the build. If you are concerned about these, use
Addressables
instead.

Additional resources