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 and USS files as objects in C#. Since both are standard Unity assets, you can use Unity's standard workflows to load them.
VisualTreeAssetStyleSheetUse serialization references
Unity automatically detects fields from your C# scripts that are of type or . 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.
VisualTreeAssetStyleSheetThere are three ways to use this in your scripts:
Description | How to display the Inspector | Reference save location |
|---|---|---|
Instances of custom scripts (such as | Select the GameObject holding the instance of the script | Inside the scene |
Default references for a script that derives from | Select the actual C# file in the Project Browser | Inside the meta file associated with the script |
Custom assets in your project that derive from | Select the asset in the Project browser | Inside the serialized data of the asset itself |
The following example class receives a UXML file and a list of USS files from the Inspector:
MonoBehaviourusing 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 class receives default references from the Inspector:
EditorWindowusing 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 class.
AssetDatabaseThe 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 folder in your project and place your UI assets in it, you can use the method to load your assets.
ResourcesResources.LoadThe following examples show how to load an asset in the folder:
ResourcesVisualTreeAsset uxml = Resources.Load<VisualTreeAsset>("main_window");StyleSheet uss = Resources.Load<StyleSheet>("main_styles");