Unity3D :使用列表视图绑定到列表
推荐:将NSDT场景编辑器就加入你的3D工具链
3D工具集:NSDT简石数字孪生
使用列表视图绑定到列表
版本: 2021.3+
控件是创建列表的最有效方法。若要使用 ListView 绑定到列表,请将 ListView 的绑定路径设置为包含该列表的属性的名称。
此示例演示如何使用 ListView 绑定到列表。
示例概述
该示例创建一个切换列表,并将该列表绑定到对象的基础列表。GameSwitch

您可以在此 GitHub 存储库中找到此示例创建的已完成文件。
先决条件
本指南适用于熟悉 Unity 编辑器的开发人员,用户界面
工具包和 C# 脚本。在开始之前,请熟悉以下内容:
- 可视化树
- 用户体验
- USS
- ListView
使用列表创建对象
创建一个对象和一个序列化对象,该对象将对象列表作为属性。GameSwitchGameSwitch
- 使用任何模板创建 Unity 项目。
- 在你的项目窗口
 ,创建一个名为以存储所有文件的文件夹。bind-to-list
创建一个名为的 C# 脚本,并将其内容替换为以下内容:GameSwitch.cs
using System;
[Serializable]
public struct GameSwitch
{
    public string name;
    public bool enabled;
}4.  创建一个名为的 UI 文档,并将其内容替换为以下内容:game_switch.uxml
<UXML xmlns="UnityEngine.UIElements" xmlns:ue="UnityEditor.UIElements">
    <Box style="flex-direction: row;">
        <Toggle binding-path="enabled" />
        <TextField binding-path="name" readonly="true" style="flex-grow: 1;"/>
    </Box>
</UXML>5. 创建一个名为的 C# 脚本,并将其内容替换为以下内容:GameSwitchListAsset.cs
using System.Collections.Generic;
using UnityEngine;
namespace UIToolkitExamples
{
    [CreateAssetMenu(menuName = "UIToolkitExamples/GameSwitchList")]
    public class GameSwitchListAsset : ScriptableObject
    {
        public List<GameSwitch> switches;
        public void Reset()
        {
            switches = new()
            {
                new() { name = "Use Local Server", enabled = false },
                new() { name = "Show Debug Menu", enabled = false },
                new() { name = "Show FPS Counter", enabled = true },
            };
        }
        public bool IsSwitchEnabled(string switchName) => switches.Find(s => s.name == switchName).enabled;
    }
}创建自定义编辑器并设置绑定
创建一个自定义编辑器,该编辑器可以创建具有切换列表的资产。通过将 UI 文档的属性设置为列表的属性名称(即 )。GameSwitchbinding-pathGameSwitchswitches
- 创建一个名为 的文件夹。Editor
在“编辑器”文件夹中,创建一个名为的 C# 脚本,并将其内容替换为以下内容:GameSwitchListEditor.cs
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
namespace UIToolkitExamples
{
    [CustomEditor(typeof(GameSwitchListAsset))]
    public class GameSwitchListEditor : Editor
    {
        [SerializeField]
        VisualTreeAsset m_ItemAsset;
        [SerializeField]
        VisualTreeAsset m_EditorAsset;
        public override VisualElement CreateInspectorGUI()
        {
            var root = m_EditorAsset.CloneTree();
            var listView = root.Q<ListView>();
            listView.makeItem = m_ItemAsset.CloneTree;
            return root;
        }
    }
}
创建一个名为的 UI 文档,并将其内容替换为以下内容:game_switch_list_editor.uxml
<UXML xmlns="UnityEngine.UIElements" xmlns:ue="UnityEditor.UIElements">
    <ListView virtualization-method="DynamicHeight"
              reorder-mode="Animated"
              binding-path="switches"
              show-add-remove-footer="true"
              show-border="true"
              show-foldout-header="true"
              header-title="Switches"
    />
</UXML>4. 在“项目”窗口中,选择“游戏切换列表编辑器.cs”。
5. 将 game_switch.uxml 拖动到项目资产检查员.
6. 将 game_switch_list_editor.uxml 拖到检查器中的编辑器资源。
测试绑定
- 从菜单中,选择“资产”>“创建> UIToolkit示例”>“游戏切换列表”。这将创建一个名为“新建游戏切换列表资产”的资产。
- 在“项目”窗口中,选择“新建游戏交换机列表资产”。这将显示检查器中的切换列表。您可以对列表重新排序、折叠列表、在列表中添加条目或删除条目,以及更改列表中的条目数。如果在检查器 UI 中进行更改,则对象的属性会更改。switchesGameSwitchListAsset
由3D建模学习工作室整理翻译,转载请注明出处!