버전:2021.3+
이 예는 UXML 템플릿으로 바인딩 경로를 설정하는 방법을 보여줍니다.
이 예제에서는 에셋 메뉴 항목을 생성합니다.이 메뉴는 GameSwitch
오브젝트의 프로퍼티에 바인딩하는 템플릿 인스턴스 세 개가 포함된 GameSwitch
에셋을 생성합니다.
이 예제에서 생성한 완성된 파일은 다음 GitHub 저장소에서 확인할 수 있습니다.
이 가이드는 Unity 에디터, UI 툴킷, C# 스크립팅에 익숙한 개발자용입니다.시작하기 전에 먼저 다음을 숙지하십시오.
GameSwitch
구조체를 정의하는 스크립트와 GameSwitch
구조체의 프로퍼티를 보유하는 커스텀 에셋을 생성합니다.
템플릿을 사용하여 Unity에서 프로젝트를 생성합니다.
프로젝트(Project) 창에서 모든 파일을 저장할 폴더를 ’bind-uxml-template’이라는 이름으로 만듭니다.
GameSwitch.cs
라는 이름의 C# 스크립트를 생성하고 콘텐츠를 다음으로 바꿉니다.
using System;
[Serializable]
public struct GameSwitch
{
public string name;
public bool enabled;
}
GameSwitchesAsset.cs
라는 이름의 C# 스크립트를 만들고 해당 콘텐츠를 다음과 같이 바꿉니다.
using UnityEngine;
[CreateAssetMenu(menuName = "UIToolkitExamples/GameSwitches")]
public class GameSwitchesAsset :ScriptableObject
{
public GameSwitch useLocalServer;
public GameSwitch showDebugMenu;
public GameSwitch showFPSCounter;
// Use the Reset function to provide default values
public void Reset()
{
useLocalServer = new GameSwitch() { name = "Use Local Server", enabled = false };
showDebugMenu = new GameSwitch() { name = "Show Debug Menu", enabled = false };
showFPSCounter = new GameSwitch() { name = "Show FPS Counter", enabled = true };
}
}
각 GameSwitch
구조체 인스턴스에 사용할 수 있는 UXML 템플릿과 이 템플릿을 사용하는 UXML 파일을 생성합니다.
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>
bind-uxml-template 폴더에 ’Editor’라는 이름의 폴더를 만듭니다.
Editor 폴더에 game_switches_editor.uxml
이라는 이름의 UI 문서를 생성하고 해당 콘텐츠를 다음과 같이 바꿉니다.
<UXML xmlns="UnityEngine.UIElements" xmlns:ue="UnityEditor.UIElements">
<Template name="switch" src="../game_switch.uxml"/>
<Instance template="switch" binding-path="useLocalServer" />
<Instance template="switch" binding-path="showDebugMenu" />
<Instance template="switch" binding-path="showFPSCounter" />
</UXML>
참고:이것은 커스텀 에디터의 기본 UXML 파일입니다.각 프로퍼티는 ’Instance’의 ‘binding-path’ 속성을 통해 ’game_switch.uxml’의 인스턴스에 바인딩됩니다.
스크립트를 생성하여 GameSwitchesAsset
에 대한 커스텀 에디터를 등록합니다.
GameSwitchesEditor.cs
라는 이름의 C# 스크립트를 생성하고 해당 콘텐츠를 다음과 같이 바꿉니다.
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
namespace UIToolkitExamples
{
[CustomEditor(typeof(GameSwitchesAsset))]
public class GameSwitchesEditor :Editor
{
[SerializeField]
VisualTreeAsset visualTreeAsset;
public override VisualElement CreateInspectorGUI()
{
return visualTreeAsset.CloneTree();
}
}
}
Assets
폴더에 새 에셋을 생성합니다.GameSwitchesAsset.useLocalServer
, GameSwitchesAsset.showDebugMenu
, GameSwitchesAsset.showFPSCounter
프로퍼티에 바인딩되는 토글과 텍스트 필드가 표시됩니다.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.