UI Toolkit简介 中介绍了 UI Builder、样式属性、UQuery、Debugger,UI Toolkit元素 中介绍了 Label、Button、TextField、Toggle、Radio Button、Slider、Progress Bar、Dropdown、Foldout 等元素,UI Toolkit样式选择器 中介绍了简单选择器、复杂选择器、伪类选择器等样式选择器,本文将介绍 UI Toolkit 中的容器,主要包含 VisualElement、ScrollView、ListView、GroupBox 等,官方介绍详见→UXML elements reference。
VisualElement 是一个空容器,便于组织和管理元素,官方介绍见→UXML element VisualElement。
1)属性介绍
说明:View Data Key、Picking Mode、Tooltip、Usage Hints、Tab Index、Focusable 都是基类属性,后文若出现这些属性将不再赘述。
2)获取根 VisualElement 容器
VisualElement rootVisualElement = GetComponent<UIDocument>().rootVisualElement;
3)注册事件回调(RegisterCallback)
RegisterCallbackDemo.cs
using UnityEngine;
using UnityEngine.UIElements;public class RegisterCallbackDemo : MonoBehaviour {private void Awake() {VisualElement rootVisualElement = GetComponent<UIDocument>().rootVisualElement;rootVisualElement.RegisterCallback<MouseDownEvent>(OnClickDown);rootVisualElement.RegisterCallback<ClickEvent>(OnClick);}private void OnClickDown(MouseDownEvent e) { // 鼠标按下时事件回调Debug.Log("mousePosition=" + e.mousePosition + ", pressedButtons=" + e.pressedButtons); // 1:左键, 2:右键, 4:中键}private void OnClick(ClickEvent e) { // 鼠标左键点击时事件回调Debug.Log("target=" + e.target);}
}
说明:注册的事件主要有以下几种,官方介绍见→Event reference。
4)添加事件操作器(AddManipulator)
ManipulatorDemo.cs
using UnityEngine;
using UnityEngine.UIElements;public class ManipulatorDemo : MonoBehaviour {private VisualElement rootVisualElement;private void Awake() {rootVisualElement = GetComponent<UIDocument>().rootVisualElement;Clickable leftClickManipulator = new Clickable(OnCtrlDoubleClicked);leftClickManipulator.activators.Clear();leftClickManipulator.activators.Add(new ManipulatorActivationFilter() {button = MouseButton.LeftMouse, // 鼠标左键clickCount = 2, // 点击次数modifiers = EventModifiers.Control // 按键});rootVisualElement.AddManipulator(leftClickManipulator);}private void OnCtrlDoubleClicked(EventBase e) { // Ctrl+Double Click事件回调Debug.Log("OnCtrlDoubleClicked");}
}
1)属性介绍
ScrollView 是一个滚动容器,官方介绍见→UXML element ScrollView。
2)添加元素
将元素拖拽到 ScrollView 上,会自动放在其 unity-content-container 元素下面,如下。
也可以通过以下代码添加元素。
VisualElement rootVisualElement = GetComponent<UIDocument>().rootVisualElement;
ScrollView scrollview = rootVisualElement.Q<ScrollView>();
scrollview.Add(new Label("LabelContent"));
ListView 是一个列表容器,官方介绍见→UXML element ListView。
1)属性介绍
2)ListView 的使用
ListViewDemo.cs
using UnityEngine;
using UnityEngine.UIElements;
using System.Collections.Generic;public class ListViewDemo : MonoBehaviour {private VisualElement root; // 根容器private ListView listView; // 列表private string[] itemsTitle = new string[] {"First", "Second", "Third", "Fourth"}; // item的标题private int[] itemsData = new int[]{0, 1, 2, 3}; // item的数值private void Awake() {root = GetComponent<UIDocument>().rootVisualElement;listView = root.Q<ListView>();listView.fixedItemHeight = 60;listView.itemsSource = itemsData;listView.makeItem += MakeItem;listView.bindItem += SelectionChange += OnSelectionChange;}private VisualElement MakeItem() { // 创建item元素, 这里以Label元素呈现itemLabel label = new Label();label.style.fontSize = 50;label.style.unityTextAlign = TextAnchor.MiddleLeft;return label;}private void BindItem(VisualElement visualElement, int index) { // 绑定itemLabel label = visualElement as = itemsTitle[index];}private void OnSelectionChange(IEnumerable<object> objs) { // 选中事件回调foreach (object item in objs) {int data = (int) item;Debug.Log(data);}}
}
运行后,点击 Second,显示如下。
打印日志如下。
GroupBox 是一个逻辑分组容器,官方介绍见→UXML element GroupBox。
1)属性介绍
2)GroupBox 的使用
GroupBoxDemo.cs
using UnityEngine;
using UnityEngine.UIElements;public class GroupBoxDemo : MonoBehaviour {private VisualElement root; // 根容器private GroupBox groupBox; // 分组盒子private string[] choiceLabel = new string[] {"First", "Second", "Third", "Fourth"}; // choice的标签private void Awake() {root = GetComponent<UIDocument>().rootVisualElement;groupBox = root.Q<GroupBox>(); = "GroupBoxDemo";groupBox.style.fontSize = 50;root.Add(groupBox);for (int i = 0; i < choiceLabel.Length; i++) {AddChoice(i);}}private void AddChoice(int index) { // 添加单选项RadioButton choice = new RadioButton(); = choiceLabel[index];choice.style.fontSize = 50;VisualElement ve = choice.Query<VisualElement>().AtIndex(2);ve.style.marginRight = 10;choice.RegisterValueChangedCallback(e => OnValueChanged(index, e));groupBox.Add(choice);}private void OnValueChanged(int index, ChangeEvent<bool> e) { // 选项变化回调函数Debug.Log("index=" + index + ", previousValue=" + e.previousValue + ", newValue=" + e.newValue);}
}
运行后,点击 Second,显示如下。
打印日志如下。
本文发布于:2024-01-31 18:58:22,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170669870530656.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |