버전:2021.3+
ListView 컨트롤은 리스트를 만드는 가장 효율적인 방법입니다.ListView를 사용하여 리스트에 바인딩하려면 ListView의 바인딩 경로를 해당 리스트가 포함된 프로퍼티의 이름으로 설정합니다.
이 예는 ListView를 사용하여 리스트에 바인딩하는 방법을 보여줍니다.
이 예제에서는 토글 리스트를 생성하고 이 리스트를 GameSwitch
오브젝트의 기본 리스트에 바인딩합니다.
이 예제에서 생성한 완성된 파일은 GitHub 저장소에 있습니다.
이 가이드는 Unity 에디터, UI 툴킷, C# 스크립팅에 익숙한 개발자용입니다.시작하기 전에 먼저 다음을 숙지하십시오.
‘GameSwitch오브젝트와
GameSwitch` 오브젝트 리스트를 프로퍼티로 가진 직렬화된 오브젝트를 생성합니다.
임의의 템플릿을 사용하여 Unity 프로젝트를 생성합니다.
프로젝트(Project) 창에서 모든 파일을 저장할 ’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을 인스펙터의 Item Asset으로 드래그합니다.
Game_switch_list_editor.uxml을 인스펙터의 Editor Asset으로 드래그합니다.
GameSwitchListAsset
오브젝트의 switches
프로퍼티가 변경됩니다.Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.