커스텀 바인딩 타입을 생성하여 런타임 바인딩 시스템을 확장할 수 있습니다. 커스텀 바인딩 타입을 생성하려면 클래스를 생성하고 CustomBinding
클래스에서 상속합니다.
CustomBinding
은 IBinding
인터페이스와 유사하며, 하나가 아닌 여러 개의 바인딩 인스턴스를 등록할 수 있게 합니다. CustomBinding
은 확장성을 위한 엔트리 포인트이며 바인딩을 업데이트하기 위한 Update
메서드만 제공합니다. 하지만 다음 메서드를 구현하여 바인딩이 등록되거나 등록 해제될 때, 그리고 요소에서 데이터 소스 컨텍스트가 변경되었을 때 콜백을 수신할 수 있습니다.
바인딩 타입에 대한 데이터 소스 및 데이터 소스 경로를 정의하려면 IDataSourceProvider
인터페이스를 구현합니다. 바인딩 시스템은 이 인터페이스에서 제공하는 dataSource
및 dataSourcePath
프로퍼티를 사용하여 확인된 데이터 소스 및 데이터 소스 경로를 결정합니다. 이러한 프로퍼티는 계층 구조에서 얻은 값을 오버라이드하기 때문에 “로컬”이라고 합니다. 중요한 점은 이러한 “로컬” 프로퍼티를 수정해도 요소 자체나 그 하위 요소에는 영향을 미치지 않는다는 것입니다.
기본적으로 바인딩 시스템은 모든 프레임에서 CustomBinding
인스턴스를 업데이트합니다.
업데이트 트리거를 정의하려면 다음 메서드를 사용합니다.
MarkDirty
: 다음 사이클 동안 업데이트되도록 바인딩 오브젝트를 dirty
로 설정합니다.updateTrigger
: 이 enum
프로퍼티를 사용하여 바인딩이 업데이트되는 방식을 변경합니다.BindingResult
: 이 메서드를 사용하여 업데이트 프로세스를 커스터마이즈합니다. BindingResult
는 업데이트가 성공했는지 여부를 알려 주는 구조체입니다. status
및 message
를 포함합니다.BindingResult
는 status
및 message
를 포함합니다. 다음은 status
에 대해 설정될 수 있는 값입니다.
BindingResult
메서드의 Pending
결과를 사용하여 다음 사이클에서 바인딩 오브젝트를 업데이트해야 하는 경우 바인딩 시스템에 알릴 수 있습니다.
이 섹션에서는 예시를 통해 UI Builder, UXML 및 C#에서 커스텀 바인딩 타입을 만들고 바인딩을 설정하는 방법을 설명합니다.
다음 예시에서는 현재 시간을 표시하는 커스텀 바인딩 타입을 생성합니다. 이를 Label의 text
프로퍼티에 바인딩하여 시계를 생성할 수 있습니다.
[!code-cs[(Modules/UIElements/Tests/UIElementsExamples/Assets/Examples/CustomBinding_CurrentTime.cs)]
커스텀 바인딩 타입을 생성하면 UI Builder의 Add binding 창에 표시됩니다. UI Builder에서 바인딩을 설정하려면 Add Binding 창의 Type 목록에서 CurrentTimeBinding을 선택합니다.
이 바인딩은 UXML에서 다음과 같이 표시됩니다.
<ui:Label text="Label">
<Bindings>
<CurrentTimeBinding property="text" />
</Bindings>
</ui:Label>
이 바인딩은 C#에서 다음과 같이 표시됩니다.
var label = new Label();
label.SetBinding("text", new CurrentTimeBinding());
다음 팁과 베스트 프랙티스를 준수하여 성능을 최적화합니다.
BindingUpdateTrigger.OnSourceChanged
사용: 소스에서 변경 사항이 감지될 때만 바인딩 타입이 업데이트를 필요로 하는 경우, updateTrigger
를 BindingUpdateTrigger.OnSourceChanged
로 설정합니다. 이렇게 하면 바인딩 타입이 필요할 때만 업데이트되어 성능을 최적화할 수 있습니다.BindingUpdateTrigger.WhenDirty
사용: 바인딩 타입을 수동으로 업데이트하며 즉각적인 동기화를 필요로 하지 않는 경우, updateTrigger
를 BindingUpdateTrigger.WhenDirty
로 설정합니다. 이렇게 하면 바인딩 타입이 업데이트되는 시기를 수동으로 제어할 수 있으므로 동기화에 대한 유연성과 제어를 강화할 수 있습니다.Update
콜백 대신 OnActivated
, OnDeactivated
또는 OnDataSourceChanged
콜백을 사용합니다. 이러한 콜백은 특정 수명 주기 이벤트에서 트리거되므로 불필요한 업데이트를 줄이고 효율성을 개선해 줍니다. 적절한 콜백을 사용하면 바인딩 타입의 동작을 최적화하고 필요할 때 정확하게 업데이트가 이루어지도록 할 수 있습니다.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.