包含此页的版本:
不含此页的版本:
想要使用 UI Toolkit 创建您的第一个 UI?使用此基本的 UI 工具包工作流示例开始使用。
注意:出于演示目的,本指南介绍了如何为编辑器UI添加UI控件。但是,有关将 UI 控件添加到 UI 文档的说明也适用于运行时 UI。有关更多信息,请参阅运行时 UI 入门。
如果您经常执行特定任务,则可以使用 UI Toolkit 为其创建专用 UI。例如,您可以创建自定义编辑器窗口。该示例演示了如何使用UI Builder、UXML和C#脚本创建自定义编辑器窗口并将UI控件添加到自定义编辑器窗口中。
您可以在此 GitHub 存储库中找到此示例创建的已完成文件。
创建一个带有两个标签的自定义编辑器窗口。
Assets文件夹,然后选择“创建> UI 工具包”>“编辑器窗口”。SimpleCustomEditor在 C# 框中。您可以在Assets/Editor文件夹。
您可以通过以下方式将 UI 控件添加到窗口:
您可以单独使用或组合使用这些方法中的任何一种。以下示例使用这些方法的组合创建三组标签、按钮和切换。
若要直观地将 UI 控件添加到窗口,请使用 UI 生成器。除了默认标签之外,以下步骤还在自定义编辑器窗口中添加按钮和切换开关。
Editor文件夹,双击SimpleCustomEditor.uxml以打开 UI Builder。These controls were created in UI Builder在“文本”字段中。This is button1在“文本”字段中。button1在“名称”字段中。Number?在标签字段中。toggle1在“名称”字段中。SimpleCustomEditor.cs并确保 可视化树资源(Visual Tree Asset) 设置为SimpleCustomEditor (Visual Tree Asset)在“检查器”窗口中。
如果你更喜欢在文本文件中定义你的UI,你可以编辑UXML以添加UI控件。以下步骤将另一组标签、按钮和切换添加到窗口中。
Editor文件夹中,选择 资产(Assets) > 创建 > UI 工具包> UI 文档(UI Document),以创建名为SimpleCustomEditor_UXML.uxml.SimpleCustomEditor_UXML.uxml在“项目”窗口中。inlineStyle打开SimpleCustomEditor_UXML.uxml在文本编辑器中。SimpleCustomEditor_uxml.uxml替换为以下内容: <ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" xsi="http://www.w3.org/2001/XMLSchema-instance" engine="UnityEngine.UIElements" editor="UnityEditor.UIElements" noNamespaceSchemaLocation="../../UIElementsSchema/UIElements.xsd" editor-extension-mode="False">
<ui:Label text="These controls were created with UXML." />
<ui:Button text="This is button2" name="button2"/>
<ui:Toggle label="Number?" name="toggle2"/>
</ui:UXML>
打开SimpleCustomEditor.cs.
导入手动创建的UXML文件,方法是将以下内容添加到CreateGUI方法:
// Import UXML created manually.
var visualTree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/Editor/SimpleCustomEditor_uxml.uxml");
VisualElement labelFromUXML = visualTree.Instantiate();
root.Add(labelFromUXML);
保存您的更改。
选择 Window > UI Toolkit > SimpleCustomEditor。这将打开你的自定义编辑器窗口,其中包含三个标签、两个按钮和两个切换开关。
如果您更喜欢编码,可以使用 C# 脚本将 UI 控件添加到窗口。以下步骤将另一组标签、按钮和切换添加到窗口中。
打开SimpleCustomEditor.cs.
Unity 使用UnityEngine.UIElements用于基本 UI 控件,如标签、按钮和切换。若要使用 UI 控件,必须添加以下声明(如果尚不存在)。
using UnityEngine.UIElements;
将现有标签的文本从"Hello World! From C#"自"These controls were created using C# code.".
EditorWindow 类有一个名为rootVisualElement.要将 UI 控件添加到窗口,请先使用一些属性实例化元素类,然后使用Add方法rootVisualElement.
您的完成CreateGUI()方法如下所示:
public void CreateGUI()
{
// Each editor window contains a root VisualElement object
VisualElement root = rootVisualElement;
// VisualElements objects can contain other VisualElements following a tree hierarchy.
Label label = new Label("These controls were created using C# code.");
root.Add(label);
Button button = new Button();
button.name = "button3";
button.text = "This is button3.";
root.Add(button);
Toggle toggle = new Toggle();
toggle.name = "toggle3";
toggle.label = "Number?";
root.Add(toggle);
// Instantiate UXML created automatically which is set as the default VisualTreeAsset.
root.Add(m_VisualTreeAsset.Instantiate());
// Import UXML created manually.
var visualTree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/Editor/SimpleCustomEditor_uxml.uxml");
VisualElement labelFromUXML = visualTree.Instantiate();
root.Add(labelFromUXML);
}
选择 Window > UI Toolkit > SimpleCustomEditor 打开自定义编辑器窗口,查看三个标签、三个按钮和三个切换开关。
可以为 UI 控件设置事件处理程序,以便在单击按钮并选择或清除切换时,UI 控件执行一些任务。
在此示例中,设置事件处理程序:
您的完成SimpleCustomEditor.cs如下所示:
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
public class SimpleCustomEditor : EditorWindow
{
[SerializeField]
private VisualTreeAsset m_VisualTreeAsset = default;
[MenuItem("Window/UI Toolkit/SimpleCustomEditor")]
public static void ShowExample()
{
SimpleCustomEditor wnd = GetWindow<SimpleCustomEditor>();
wnd.titleContent = new GUIContent("SimpleCustomEditor");
}
private int m_ClickCount = 0;
private const string m_ButtonPrefix = "button";
public void CreateGUI()
{
// Each editor window contains a root VisualElement object
VisualElement root = rootVisualElement;
// VisualElements objects can contain other VisualElement following a tree hierarchy.
Label label = new Label("These controls were created using C# code.");
root.Add(label);
Button button = new Button();
button.name = "button3";
button.text = "This is button3.";
root.Add(button);
Toggle toggle = new Toggle();
toggle.name = "toggle3";
toggle.label = "Number?";
root.Add(toggle);
// Instantiate UXML created automatically which is set as the default VisualTreeAsset.
root.Add(m_VisualTreeAsset.Instantiate());
// Import UXML created manually.
var visualTree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/Editor/SimpleCustomEditor_uxml.uxml");
VisualElement labelFromUXML = visualTree.Instantiate();
root.Add(labelFromUXML);
//Call the event handler
SetupButtonHandler();
}
//Functions as the event handlers for your button click and number counts
private void SetupButtonHandler()
{
VisualElement root = rootVisualElement;
var buttons = root.Query<Button>();
buttons.ForEach(RegisterHandler);
}
private void RegisterHandler(Button button)
{
button.RegisterCallback<ClickEvent>(PrintClickMessage);
}
private void PrintClickMessage(ClickEvent evt)
{
VisualElement root = rootVisualElement;
++m_ClickCount;
//Because of the names we gave the buttons and toggles, we can use the
//button name to find the toggle name.
Button button = evt.currentTarget as Button;
string buttonNumber = button.name.Substring(m_ButtonPrefix.Length);
string toggleName = "toggle" + buttonNumber;
Toggle toggle = root.Q<Toggle>(toggleName);
Debug.Log("Button was clicked!" +
(toggle.value ? " Count: " + m_ClickCount : ""));
}
}