Load UXML and USS C# scripts
Find visual elements with UQuery
Instantiate UXML from C# scriptsTo build UI from a UXML file, you must first load the file into a VisualTreeAsset
, and then use the Instantiate()
to instantiate without a parent, which creates a new TemplateContainer
, or CloneTree(parent)
) to clone inside a parent.
Once the UXML is instantiated, you can retrieve specific elements from the visual treeAn object graph, made of lightweight nodes, that holds all the elements in a window or panel. It defines every UI you build with the UI Toolkit.
See in Glossary with UQuery.
The following example creates a custom Editor window and loads a UXML file as its content:
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
using UnityEditor.UIElements;
public class MyWindow : EditorWindow {
[MenuItem ("Window/My Window")]
public static void ShowWindow () {
EditorWindow w = EditorWindow.GetWindow(typeof(MyWindow));
VisualTreeAsset uiAsset = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/MyWindow.uxml");
VisualElement ui = uiAsset.Instantiate();
w.rootVisualElement.Add(ui);
}
}
Additional resources
Load UXML and USS C# scripts
Find visual elements with UQuery
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4