Unity Extensible Markup Language(UXML)
UXML
啟發於HTML
,XAML
與XML
,它定義並保存了UI的結構,可以使用Inline Style
或是透過Unity Style Sheet
來更改UI的風格(Style)。
UXML使用了類似樹狀的結構來保存UI,它存在父子關係(parent-child relationships)以及一個根元素(root element)
以下是UXML
的範例
1 |
|
- UXML declaration:在上面範例的第一行,是
UXML declaration
,它是可選的,- 如果你使用
UXML declaration
的話,必須把它放在第一行, - 使用
UXML declaration
的話,必須為它設定version
; encoding
則是可選的,但是如果使用encoding
的話,必須指定它的編碼。
- 如果你使用
- Document root:
<UXML>
為文件的根element,在此定義namespace與schema definition files的位置。- Namespace:在UI Toolkit中每個element的Namespace不是在
UnityEngine.UIElements
就是在UnityEditor.UIElements
。UnityEngine.UIElements
:此命名空間下的elements會作為Unity runtime時的一部分UnityEditor.UIElements
:此命名空間下的elements可在Unity Editor使用- 在使用時,你可以指定element的namespace,例如:
<UnityEngine.UIElements:Button />
- 或是在
UXML declaration
中指定它的prefix,如xmlns:engine="UnityEngine.UIElements"
,在使用時會變為<engine:Button />
- 注意:不要把自訂的control class放到
UnityEngine.UIElements
與UnityEditor.UIElements
不然UI Builder會把你自訂的control隱藏。
- Schema definition:它指定了每個UXML element可以包含哪些attributes和子element。
- Namespace:在UI Toolkit中每個element的Namespace不是在
所有Element都會繼承VisualElement
,這個VisualElement
提供了以下attributes
- name: 用來辨認這個element,它的名稱應該要唯一。
- picking-mode: 設定為
Position
表示可以回應滑鼠事件,設定為Ignore
表示忽略滑鼠事件 - focus-index: (OBSOLETE過時了) Use tabIndex and focusable.
- tabindex: 一個整數,定義這個element的tabbing position
- focusable: 一個Boolean,表示這個element是focusable.
- class: 類似HTML的class,你可以為特定的element加上class,讓它們應用到指定的style;你也可以把它當做篩選element的工具,透過UQuery來找到特定class的elements。
- tooltip: 當滑鼠指到該element時,會顯示提示字串
- view-data-key : 一個字串,作為element序列化(serialization)時使用的Key
使用Unity建立UXML:
- 選擇
Asset
->Create
->UI Toolkit
->UI Document
- 預設它會幫你建立一個noNamespaceSchemaLocation
1 | <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"> |
Reference: https://docs.unity3d.com/Manual/UIE-WritingUXMLTemplate.html