unity-game-enginedata-bindingui-toolkit

Unity-UI Toolkit Binding multiple fields


I was wondering if there is a way how to bind data to multiple properties on the same Visual Element or is it strictly that one element can only have one binding-path at any given time? Something similar what you can achieve in C#/WPF.

lets say I have class:

 public class Player
 {
     public int lives;
     public bool isAlive;
 }

WPF Binding:

 <Label Content={Binding lives} Visibility={Binding isAlive} />

UXML Bidning

<ui:Label tabindex="-1" binding-path="lives"  />

Now my Label in uxml correctly display lives and auto-updates, but I have no clue how to hook visibility to isAlive property.

I know I can do this in C#:

 var root = GetComponent<UIDocument>().rootVisualElement;
 var livesLabel = root.Q<Label>("labelName");
 livesLabel.visibility = myPlayerInstance.isAlive;

But when I have more complicated binding I can just set the values for the whole thing by hand in C#.


Solution

  • You could move your properties to a struct and then bind them by using a Bindable Element like how they did in this example here