版本: 2021.3+
ListView 控件是创建列表最有效的方式。要使用 ListView 绑定到列表,请将 ListView 的绑定路径设置为包含该列表的属性的名称。
此示例演示如何使用 ListView 绑定到列表。
此示例创建了一个切换按钮列表,并将该列表绑定到 GameSwitch 对象的基础列表。
 
您可以在此 GitHub 存储库 中找到此示例创建的完整文件。
本指南适用于熟悉 Unity 编辑器、UI(用户界面) 允许用户与您的应用程序交互。Unity 目前支持三种 UI 系统。 更多信息
参见 术语表 工具包和 C# 脚本的开发人员。在开始之前,请熟悉以下内容
创建一个 GameSwitch 对象和一个序列化对象,该对象具有一个 GameSwitch 对象列表作为属性。
使用任何模板创建一个 Unity 项目。
在您的 项目窗口显示 Assets 文件夹内容的窗口(项目选项卡) 更多信息
参见 术语表 中,创建一个名为 bind-to-list 的文件夹以存储所有文件。
创建一个名为 GameSwitch.cs 的 C# 脚本,并将其内容替换为以下内容
using System;
[Serializable]
public struct GameSwitch
{
    public string name;
    public bool enabled;
}
创建一个名为 game_switch.uxml 的 UI 文档,并将其内容替换为以下内容
<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>
创建一个名为 GameSwitchListAsset.cs 的 C# 脚本,并将其内容替换为以下内容
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 文档的 binding-path 属性设置为 GameSwitch 列表的属性名称(即 switches)来将切换按钮列表绑定到 GameSwitch 列表。
创建一个名为 Editor 的文件夹。
在 Editor 文件夹中,创建一个名为 GameSwitchListEditor.cs 的 C# 脚本,并将其内容替换为以下内容
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;
        }
    }
}
创建一个名为 game_switch_list_editor.uxml 的 UI 文档,并将其内容替换为以下内容
<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>
在项目窗口中,选择 GameSwitchListEditor.cs。
将 game_switch.uxml 拖动到 检视器一个 Unity 窗口,显示当前选定 GameObject、资产或项目设置的信息,允许您检查和编辑值。 更多信息
参见 术语表 中的 Item Asset。
将 game_switch_list_editor.uxml 拖动到检视器中的 Editor Asset。
GameSwitchListAsset 对象的 switches 属性也会发生变化。