Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    Dismiss Notice

[UIElements] Binding

Discussion in 'UI Toolkit' 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
    a436t4ataf likes this.
  2. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,065
    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:
     
    Olakehs likes this.
  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.
     
  4. a436t4ataf

    a436t4ataf

    Joined:
    May 19, 2013
    Posts:
    1,297
    Just to note: 2 years later and (still) little or none of this is described in the main docs page on Binding. It's magic-code and non-examples, with no explanation of what is happening, why, or how to use it (just trivial examples that can't work on non-trivial cases).

    Also, while I'm personally OK using SP/SO, in general SerializedProperty/SerializedObject add boilerplate code to your classes (and the benefits often aren't valuable, depending on the project), so are still avoided by a lot of people. Even if it's been designed for working with SP/SO (which is fine), it still needs to be documented for plain-old-C# code.

    (https://docs.unity3d.com/2020.2/Documentation/Manual/UIE-Binding.html)
     
    viridisamor likes this.
  5. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,085
    The docs are still pretty sparse on how binding for specific controls work. e.g. PopupField.

    PopupField seems to support some sort of binding, but it's a little confusing to understand what exactly. It seems to support binding to an Enum serialized property. I assume this means it'll assign the enum value index correctly as the selected value and display the enum names as options, but not entirely sure. Does it use a consecutive index or does it respect enums with indices like Red = 0, Blue = 5, Green = 7?

    I'd actually like to use automatic binding for custom class/structs as well, but that doesn't seem possible. I can pass List<MyClass> to the options, but it runs into a lot of issues with serialization (internally it compared the selected instance with the list of instances, but after deserialization, the instances are different than before).

    I'd also like to bind options separately from the selected value, for example, List<string> and another string property to hold the selected value. Alternatively, the selected index, maybe that would be even better.
     
  6. a436t4ataf

    a436t4ataf

    Joined:
    May 19, 2013
    Posts:
    1,297
    Binding sucks, and the team needs to write the documentation.

    For PopupField in particular: be aware that they use .ToString() as a key (!) and you better make sure that every item has a unique value for that C# method, or else things go badly wrong.

    (is this documented? Of course not! Why document the fact you've hardcoded your class to rely upon a officially "display" only method https://docs.microsoft.com/en-us/dotnet/api/system.object.tostring?view=net-5.0 :))
     
    Last edited: Feb 27, 2021
    RoaBen and JasonC_ like this.
unityunity