Unity中的UI工具包(UI Toolkit)不但可以用于创建编辑器UI,同样可以来创建运行时UI。
关于Unity中的UI系统以及使用UI工具包创建编辑器UI可以参见:
1. Unity中的UI系统
本文将通过一个简单示例来介绍如何使用UI工具包来创建运行时UI。
一、创建UI Document(.uxml)
1. 使用任意模板新建一个Unity项目。
2. 在Project中,右键点击Scene,选择Create > UI Toolkit > UI Document,命名为SimpleRuntimeUI。
3. 在Project里,点击SimpleRuntimeUI右侧箭头展开后,双击inlineStyle,在文本编辑器里打开。并将文件内容替换为下面代码。
<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:VisualElement style="flex-grow: 1;"> <ui:Label text="This is a Label" display-tooltip-when-elided="true"/> |