Can someone explain "Binding" to me in the context of UI Toolkit?

You can bind to any field that is backed by Serialization, which includes the Editor’s fields. If you need to change the data, you can write the final result to a field and bind to that. If you’re on 2023.2 (and newer), you can use the new binding system for UI which allows for custom modifiers for your bindings. More here: https://docs.unity3d.com/2023.2/Documentation/Manual/UIE-comparison-binding.html

Only if the TextField’s name is not unique enough. This is why it’s recommended to use the BEM pattern for naming elements and classes so you can do a single query: https://getbem.com/naming/

This is not the same as view binding that I see when working with Android UI right.

Would be convenient to have some Attribute that automatically fills up fields of a class with their UXML counterparts. So that I don’t have to root.Q finding for each element.

1 of 2 replies.

You’ll have to forgive if this is all wrong, like most things this seems to be stumbling through documentation, experimentation and shouting into the void.

  1. Create a Scriptable Object – so new MonoBehaviour Script
    9743614--1394113--upload_2024-4-1_22-20-49.png

Forgive the names – this one is CatDataScriptableObject – to bind a progress bar to how fast it’s running.

[CreateAssetMenu(fileName = "Data", menuName = "ScriptableObjects/CatDataScriptableObject", order =1)]
public class CatDataScriptableObject : ScriptableObject
{
    public float RunSpeed;
}

Because of the attributes, I can now create an instance of this with:

In the inspector I can see that is has my property.

9743614--1394119--upload_2024-4-1_22-23-33.png

In a script that I have for my UI – I add a field for this.

public class UI_Script : MonoBehaviour
{
    public PlayerController playerController;

    // Expose this as a property.
    public CatDataScriptableObject catDataScriptableObject;
...

and at some point in the code, I update the value. Back to the inspector I drag the instance I created above into this.

9743614--1394122--upload_2024-4-1_22-25-38.png

My UI Toolkit Document a simple progress bar.

9743614--1394125--upload_2024-4-1_22-27-13.png

Continued in post 2…

1 Like

Post 2 of 2.

9743617--1394131--upload_2024-4-1_22-29-21.png

You can right click on the Value and select add Binding.

9743617--1394128--upload_2024-4-1_22-29-0.png

And select the CatInstance from the Assets folder.

This seems to work…
9743617--1394134--upload_2024-4-1_22-30-29.png

I’m calling it a night tonight, but if I find a better way I will post back.

1 Like