首页 >> 大全

【Unity3D】UI Toolkit容器

2023-12-31 大全 32 作者:考证青年

1 前言

UI 简介中介绍了 UI 、样式属性、、,UI 元素中介绍了 Label、、、、Radio 、、 Bar、、 等元素,UI 样式选择器中介绍了简单选择器、复杂选择器、伪类选择器等样式选择器,本文将介绍 UI 中的容器,主要包含 、、、 等,官方介绍详见→UXML 。

(空容器)

是一个空容器,便于组织和管理元素,官方介绍见→UXML 。

1)属性介绍

说明:View Data Key、 Mode、、Usage Hints、Tab Index、 都是基类属性,后文若出现这些属性将不再赘述。

2)获取根 容器

VisualElement rootVisualElement = GetComponent().rootVisualElement;

3)注册事件回调()

.cs

using UnityEngine;
using UnityEngine.UIElements;public class RegisterCallbackDemo : MonoBehaviour {private void Awake() {VisualElement rootVisualElement = GetComponent().rootVisualElement;rootVisualElement.RegisterCallback(OnClickDown);rootVisualElement.RegisterCallback(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 。

4)添加事件操作器()

.cs

using UnityEngine;
using UnityEngine.UIElements;public class ManipulatorDemo : MonoBehaviour {private VisualElement rootVisualElement;private void Awake() {rootVisualElement = GetComponent().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");}
}

3 (滚动容器)

1)属性介绍

是一个滚动容器,官方介绍见→UXML 。

2)添加元素

将元素拖拽到 上,会自动放在其 unity-- 元素下面,如下。

也可以通过以下代码添加元素。

VisualElement rootVisualElement = GetComponent().rootVisualElement;
ScrollView scrollview = rootVisualElement.Q();
scrollview.Add(new Label("LabelContent"));

4 (列表)

是一个列表容器,官方介绍见→UXML 。

1)属性介绍

2) 的使用

.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().rootVisualElement;listView = root.Q();listView.fixedItemHeight = 60;listView.itemsSource = itemsData;listView.makeItem += MakeItem;listView.bindItem += BindItem;listView.onSelectionChange += 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 Label;label.text = itemsTitle[index];}private void OnSelectionChange(IEnumerable objs) { // 选中事件回调foreach (object item in objs) {int data = (int) item;Debug.Log(data);}}
}

运行后,点击 ,显示如下。

打印日志如下。

5 (分组盒子)

是一个逻辑分组容器,官方介绍见→UXML 。

1)属性介绍

2) 的使用

.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().rootVisualElement;groupBox = root.Q();groupBox.text = "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();choice.text = choiceLabel[index];choice.style.fontSize = 50;VisualElement ve = choice.Query().AtIndex(2);ve.style.marginRight = 10;choice.RegisterValueChangedCallback(e => OnValueChanged(index, e));groupBox.Add(choice);}private void OnValueChanged(int index, ChangeEvent e) { // 选项变化回调函数Debug.Log("index=" + index + ", previousValue=" + e.previousValue + ", newValue=" + e.newValue);}
}

运行后,点击 ,显示如下。

打印日志如下。

关于我们

最火推荐

小编推荐

联系我们


版权声明:本站内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 88@qq.com 举报,一经查实,本站将立刻删除。备案号:桂ICP备2021009421号
Powered By Z-BlogPHP.
复制成功
微信号:
我知道了