버전: 2021.3+
ListView 컨트롤은 리스트를 생성하는 가장 효율적인 방법입니다. ListView를 사용하여 리스트에 바인딩하려면 ListView의 바인딩 경로를 해당 리스트가 포함된 프로퍼티의 이름으로 설정합니다.
이 예는 ListView를 사용하여 리스트에 바인딩하는 방법을 보여 줍니다.
이 예시에서는 토글 리스트를 생성하고 이 리스트를 GameSwitch 오브젝트의 기본 리스트에 바인딩합니다.
이 예시에서 만든 완성된 파일은 이 GitHub 저장소에서 찾을 수 있습니다.
이 가이드는 Unity 에디터, UI 툴킷, C# 스크립팅에 익숙한 개발자를 위한 가이드입니다. 시작하기 전에 먼저 다음을 숙지하십시오.
GameSwitch 오브젝트와 GameSwitch 오브젝트 리스트를 프로퍼티로 가진 직렬화된 오브젝트를 생성합니다.
임의의 템플릿을 사용하여 Unity 프로젝트를 생성합니다.
프로젝트 창에서 모든 파일을 저장할 bind-to-list라는 폴더를 만듭니다.
GameSwitch.cs라는 이름의 C# 스크립트를 생성하고 콘텐츠를 다음과 같이 바꿉니다.
using System;
[Serializable]
public struct GameSwitch
{
public string name;
public bool enabled;
}
game_switch.uxml이라는 이름의__ UI__(사용자 인터페이스) 사용자가 애플리케이션과 상호 작용하도록 해 줍니다. Unity는 현재 3개의 UI 시스템을 지원합니다. 자세한 정보
See in Glossary 문서를 생성하고 해당 콘텐츠를 다음과 같이 바꿉니다.
<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 속성을 switches인 GameSwitch 리스트의 프로퍼티 이름으로 설정하여 토글 리스트를 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을 인스펙터의 Item Asset으로 드래그합니다.
game_switch_list_editor.uxml을 인스펙터의 Editor Asset으로 드래그합니다.
GameSwitchListAsset 오브젝트의 switches 프로퍼티가 변경됩니다.