要从 UXML 文件构建__ UI__(即用户界面,User Interface)让用户能够与您的应用程序进行交互。Unity 目前支持三种 UI 系统。更多信息
See in Glossary,必须首先将文件加载到 VisualTreeAsset 中,然后使用 Instantiate() 在没有父项的情况下进行实例化(这会创建一个新 TemplateContainer 或 CloneTree(parent))以便在父项中克隆。
实例化 UXML 后,可使用 UQuery 从视觉树中检索特定元素。
以下示例将创建一个自定义编辑器窗口,并加载 UXML 文件作为其内容:
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);
}
}