Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Runtime binding clarification

Discussion in 'UI Toolkit' started by nostyleguy, May 29, 2020.

  1. nostyleguy

    nostyleguy

    Joined:
    Jul 27, 2012
    Posts:
    16
    I apologize because I am sure this information is available somewhere, but I just can't seem to find it.

    For completeness, I am using Unity 2019.3 with UIElements runtime 0.0.4-preview and UIBuilder 0.11.2-preview.

    What is the current state of binding UIElements controls to data in scripts at runtime?

    All the examples I have found seem to use API that is not present in my version of UnityEngine.UIElements such as this thread:

    https://forum.unity.com/threads/setting-the-binding-path-to-a-value-inside-a-struct.839182/

    or the main documentation page for data binding:

    https://docs.unity3d.com/Manual/UIE-Binding.html

    That Manual page implies that some of the runtime controls are bindable, but I do not see a "Bind" method on either my SliderInt or root VisualElement objects like that page prescribes.

    I downloaded the Tanks demo from Unite Copenhagen, and found that it does not do real data binding for the labels in the game UI (Kills, speed, etc). Instead, it manually sets the labels each time the corresponding data value changes. I thought this was exactly the use-case for data binding, but maybe I'm completely off base.

    The only API related to binding I find on my runtime controls is ".bindingPath" and ."binding". The latter of which is an IBinding interface, but I am not sure exactly how to set, or use, it. The IBinding documentation is virtually non-existent, so if somebody could provide an example, or other docs, I would be very happy

    Thanks!
     
    EZaca likes this.
  2. uMathieu

    uMathieu

    Unity Technologies

    Joined:
    Jun 6, 2017
    Posts:
    384
    Binding is unfortunately only available for EditorWindows and custom inspectors. At the moment, you'll need to manage reading from your data and reacting to ChangeEvent<> from your fields manually. This is something we'll provide in the future
     
    EZaca likes this.
  3. nostyleguy

    nostyleguy

    Joined:
    Jul 27, 2012
    Posts:
    16
    Gotcha. Thanks for the quick response
     
  4. perholmes

    perholmes

    Joined:
    Dec 29, 2017
    Posts:
    295
    When is runtime UI binding planned for? My release date is 1-2 years in the future, and if this is in the pipeline, I might hold off on making a binding infrastructure from scratch.
     
    CodeSmile and grizzlycorv like this.
  5. grizzlycorv

    grizzlycorv

    Joined:
    Nov 9, 2018
    Posts:
    19
    +1
    It would be nice to know when runtime binding will be available.
     
    CodeSmile and ModLunar like this.
  6. ModLunar

    ModLunar

    Joined:
    Oct 16, 2016
    Posts:
    372
    Absolutely, I would really love runtime bindings as well.
    I'm so excited to be using more and more of UI Toolkit/UI Builder, it's so friendly to use :)
     
  7. BackgroundMover

    BackgroundMover

    Joined:
    May 9, 2015
    Posts:
    209
    The 2020 Q3 roadmap post specifies Runtime Binding for 2021.2, but I think that is probably superceeded by this post
     
    Ziflin likes this.
  8. Ziflin

    Ziflin

    Joined:
    Mar 12, 2013
    Posts:
    132
    Unless I missed it, there's nothing about this in the docs. I hope others avoid wasting time realizing that all the Bind() methods are in the UnityEditor.UIElements namespace as extension methods.
     
    gareth_untether and ModLunar like this.
  9. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,008
    Bump clarification.

    Felt also into a trap of IBinding interface.

    So as for now for play mode, there is no better way than using name of the UIElement and retrieving it by querying for it using Q<T> etc.. methods and than subscribing to it's events ?
     
    ModLunar likes this.
  10. griendeau_unity

    griendeau_unity

    Unity Technologies

    Joined:
    Aug 25, 2020
    Posts:
    230
    Yes it is still the case. For now, in runtime, users need to query VisualElements, set the value manually and register to value changed events.

    The team is actively working on the new Binding system which will work the same way in runtime and in editor. Stay tuned!
     
    Last edited: May 4, 2022
  11. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,006
    JakobAnarkyLabs likes this.
  12. EZaca

    EZaca

    Joined:
    Dec 9, 2017
    Posts:
    31
    I don't know how up-to-date is this thread, but it seems the runtime binding isn't a thing yet. One possible thing for now, is to create something like an "observed field" class which you can connect to the element. When it is set, the element is updated, and when the element is changed, the value is updated.

    Of course, you would need to initialize the property manually to make the binds, but it is better than the 100% manual approaches I've seen. You could even take advantage of the new inflated generics serialization and make a field like
    [SerializedField] ObservedField<string> userName;


    What I propose is:
    Code (CSharp):
    1. public class ObservedField<T>{
    2.   public T Value {
    3.     get => value;
    4.     set => SetValue(value);
    5.   }
    6.  
    7.   public event Action<T> Changed;
    8.  
    9.   public void Bind(INotifyValueChanged<T> element){
    10.     // here you use the RegisterValueChangedCallback,
    11.     // the Changed event of this class, and set the initial value
    12.   }
    13.  
    14.   private void SetValue(T value){
    15.     this.value = value;
    16.     Changed?.Invoke(value);
    17.   }
    18. }
    Would it be so different than using a Bind method from the UIElements itself?
     
    Preliy, tachweave and ModLunar like this.
  13. SamuelAsherRivello

    SamuelAsherRivello

    Joined:
    Jan 16, 2011
    Posts:
    36
    Great solutions.

    I am a custom Unity MVC framework
    https://github.com/SamuelAsherRivello/rmc-mini-mvcs/

    And its related Udemy course
    https://bit.ly/mvc-architecture-for-unity-on-udemy

    On of the included samples does a very simplistic version of bespoke binding (not systematic, reusable binding). I share here to show a low-fi alternative to the above.

    //Demo
    https://github.com/SamuelAsherRivello/rmc-mini-mvcs/tree/main/RMC Mini MVCS/Samples~/RMC Mini MVCS Examples/Examples/More/Example07_DataBindingMini/WithMini/Scripts/Runtime/Mini/View

    //Core Class
    https://github.com/SamuelAsherRivello/rmc-mini-mvcs/blob/main/RMC Mini MVCS/Runtime/RMC/Core/Architectures/Mini/Context/Observable.cs

    For my (academic) uses, that solution is fine. I saw a video that in Unity 2023.x there will be first-party runtime binding. When that is available I'll use it instead.