Search Unity

[UIElements] Binding

Discussion in 'UIElements' started by Devi-User, Oct 19, 2018.

  1. Devi-User

    Devi-User

    Joined:
    Apr 30, 2016
    Posts:
    61
    I tried to understand the question about data binding, but it turned out to be too complicated.

    I found that there are Bind / Unbind methods in VisualElement. Unfortunately, these methods work with SerializedObject and are not suitable for binding other data.
    I also found the IBindable interface, IBinding, and the BindProperty method, which also implements the binding. But SerializedProperty is an object derived from SerializedObject, which also does not suit me. What scares me most of all in such a matter is the binding for arrays, and the need to synchronize them later.

    Who, where and how carries out this binding a riddle for me.

    And I do not understand the boundaries of binding. What exactly should it be able to do within this system? I assume that this should give the opportunity to both read and assign data to the fields automatically. For example, if I have a Label and some text that I want to substitute there, then it will be replaced if the field value of the class with which I associated it changes. Or if I have an IntField, then it allows you to both read and write the value in the specified int field. Confirm or disprove this, please.

    Quite often, we are not dealing with any data set stored within a SerializedObject. Is there any way to bind data if I just store it in an instance of a class?

    I also looked at the examples provided by Unity, but it gave me a rather vague idea of how to use it in real tasks and in cases where we have third-party serialization.
     
    Last edited: Oct 19, 2018
  2. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    221
    Binding in UIElements was primarily designed to work with SerializedObjects and SerializedProperties. This is the primary way binding is done in IMGUI as well. Most data in Unity proper is stored within Serializable objects (things like ScriptableObject, GameObject, MonoBehaviour, etc), so this lets you bind UI to any those objects with ease.

    To confirm, yes, you can bind an IntField value to a field on a SerializedObject and the values will be kept in sync between the two.

    That said, there's not much magic happening here. You can still do custom binding yourself to your own serialization system. Basically, you detect value changes from the UI via ChangeEvent<>'s, and you can use the built-in UIElements scheduler to monitor for changes on the model. Obviously, if you already know when your model changes and already have those hooks, simply update the UI values accordingly.

    This is covered in the intro video posted at the top of this forum, here:
     
  3. Devi-User

    Devi-User

    Joined:
    Apr 30, 2016
    Posts:
    61
    wow, that's cool! Thanks, this is exactly what I need.
    Forgive me my carelessness. I will definitely look at it before asking questions here next time.