버전: 2023.2+
런타임 데이터 바인딩을 생성하는 방법이 궁금하신가요? 이 예시를 사용하여 시작할 수 있습니다. 이 예시에서는 데이터 소스 에셋을 생성하고 UI Builder를 사용하여 데이터 소스를 UI에 바인딩합니다.
이 예시에서는 다음을 보여 줍니다.
이 예시에서 생성하는 완성된 파일은 GitHub 저장소에서 확인할 수 있습니다.
이 가이드는 Unity 에디터, UI 툴킷, C# 스크립팅에 익숙한 개발자용입니다.시작하기 전에 먼저 다음을 숙지하십시오.
바인딩할 프로퍼티를 포함하는 데이터 소스 에셋을 생성합니다. 이 예시에서는 string
프로퍼티, Vector3
프로퍼티, float
프로퍼티를 포함하는 ExampleObject
라는 ScriptableObject
에셋을 생성합니다. float
프로퍼티는 Vector3
프로퍼티의 x
, y
, z
값의 합을 반환하는 읽기 전용 프로퍼티입니다. 또한 데이터 소스 에셋은 float
를 Color
및 string
으로 전환하는 로컬 컨버터를 포함합니다.
템플릿을 사용하여 Unity에서 프로젝트를 생성합니다.
프로젝트의 Assets
폴더에서 다음 콘텐츠가 포함된 ExampleObject.cs
라는 C# 스크립트를 생성합니다.
[!code-cs[(Modules/UIElements/Tests/UIElementsExamples/Assets/ui-toolkit-manual-code-examples/runtime-data-binding/ExampleObject.cs)]
메뉴에서 Assets > Create > Example Object를 선택합니다. 이렇게 하면 프로젝트의 Assets
폴더에 에셋이 생성됩니다.
에셋의 이름을 ExampleObject.asset
으로 변경합니다.
이전 단계에서 생성한 데이터 소스 에셋에 바인딩되는 UI를 생성합니다. 이 예제에서는 Vector3Field, FloatField 및 Label을 자식 요소로 갖는 VisualElement를 포함하는 UXML 파일을 생성합니다.
Assets
폴더에서 ExampleObject.uxml
이라는 UXML 파일을 생성합니다.ExampleObject.uxml
파일을 더블 클릭하여 UI Builder에서 엽니다.이전 단계에서 생성한 데이터 소스 에셋에 UI를 바인딩합니다. 루트 VisualElement에 대한 데이터 소스를 설정하고 Vector3Field 요소의 label
및 value
프로퍼티를 데이터 소스 에셋의 vector3Label
및 vector3Value
프로퍼티에 바인딩합니다.
Float의 value
프로퍼티를 데이터 소스 에셋의 sumOfVector3Properties
프로퍼티에 바인딩합니다.
Label의 프로퍼티를 데이터 소스 에셋의 프로퍼티에 바인딩하고 Label에 대한 타입 컨버터를 적용합니다.
계층 구조 패널에서 Label을 선택합니다.
인스펙터 패널에서 Text 프로퍼티를 오른쪽 클릭하고 Add binding을 선택합니다.
Add Binding 창에서 다음을 수행합니다.
Add binding을 선택하여 변경 사항을 저장합니다.
인스펙터 패널에서 Background 필드를 확장합니다.
Color 프로퍼티를 오른쪽 클릭하고 Add binding을 선택합니다.
Add Binding 창에서 다음을 수행합니다.
Add binding을 선택하여 변경 사항을 저장합니다.
저장하고 UI Builder를 닫습니다. ExampleObject.uxml
파일이 다음과 같이 표시됩니다.
[!code-cs[(Modules/UIElements/Tests/UIElementsExamples/Assets/ui-toolkit-manual-code-examples/runtime-data-binding/ExampleObject.uxml)]
데이터 소스 에셋에서 Vector3Field의 레이블 텍스트를 업데이트하면 UI에서 변경 사항이 모사되는 것을 확인할 수 있습니다. UI에서 Vector3 프로퍼티의 값을 업데이트하면 데이터 소스 에셋에 모사된 변경 사항을 확인할 수 있습니다. 데이터 소스에서 위험 레벨의 슬라이더를 옮겨 레이블에 모사된 변경 사항을 확인할 수 있습니다.
Assets
폴더에서 ExampleObject.asset
파일을 선택합니다.ExampleObject.asset
의 인스펙터 창에 있는 Vector3 Value가 입력한 숫자로 변경됩니다.ExampleObject.asset
의 인스펙터 창에서 Danger Level 슬라이더를 옮깁니다. UI 빌더 뷰포트에서 Label의 텍스트와 배경색이 Danger Level의 값에 따라 변경됩니다.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.