Search Unity

[RELEASED] Data Bind for Unity

Discussion in 'Assets and Asset Store' started by coeing, Feb 16, 2015.

  1. coeing

    coeing

    Joined:
    Mar 17, 2011
    Posts:
    271
    Hi @hananet0282,

    Thanks for trying Data Bind!

    Without knowing your exact use case it is hard to say what will solve your problem. But you can have a look at
    Code (CSharp):
    1. DictionaryLookup
    , it is a mono behaviour with which you can access a data dictionary of a context. Hope that helps! If not, please provide a small example so I can see what you are trying to do :)
     
  2. Asse1

    Asse1

    Joined:
    Jan 9, 2013
    Posts:
    89
    I've updated from Unity 2019 to Unity 2020.3.40f1 and the following problem appeared.

    If have a Button which uses the following structure:
    - GameObject
    - GameObject with CanvasGroup
    - GameObject with TextMeshProText UI
    - Rest are DataBind objects

    DataBind should control the Alpha value of the CanvasGroup. I use a BooleanSwitch which returns 0.2 or 0.6 and a CanvasGroupAlphaSetter that references the CanvasGroup and BooleanSwitch as Provider

    Now in the editor everything works fine but on Android the button is always fully opaque. I've tried setting the Alpha of the CanvasGroup to something transparent but it's always opaque. Only when I remove the DataBind script the button stays transparent.

    Maybe BooleanSwitch interpretes 0.6 and 0.2 wrong or something...

    I'm using the latest version of DataBind.

    Thanks already for your help!
     
  3. coeing

    coeing

    Joined:
    Mar 17, 2011
    Posts:
    271
    Hi @Asse1,

    Sorry for the late reply, I was on vacation last week.

    There is no difference between the platforms, so it sounds very strange to me that it works for you in the editor but not on Android. Are you sure it is caused by the Data Bind scripts?

    I have an Android device here as well, so please send me a small example project with which I can reproduce the issue.

    Cheers
    Christian
     
  4. Asse1

    Asse1

    Joined:
    Jan 9, 2013
    Posts:
    89
    Hey @coeing,

    in the end Convert.ChangeType() seems to be culture-independent in Unity 2019 and is dependent in Unity 2020.

    That's why after upgrading Unity to 2020 everything worked on my laptop (where culture info was resolved to en-us) and not on my phone (where it was resolved to de-de).

    So in the editor "0.5" was converted to 0.5 but on my phone it was converted to 1.

    Best,
    Ruben
     
    coeing likes this.
  5. coeing

    coeing

    Joined:
    Mar 17, 2011
    Posts:
    271
    Hi Ruben!

    I see, great find! I will see what I can do about it for the next version. I added an issue to the data bind issue tracker: https://bitbucket.org/coeing/data-bind/issues/105/booleanswitch-handles-floats-different-in

    Thanks for your report!

    Cheers
    Christian
     
  6. wdc_bigbluegames

    wdc_bigbluegames

    Joined:
    May 23, 2022
    Posts:
    13
    Hello @coeing Thanks for writing this library, we're investigating it for usage in one of our titles. In the past we have written our own DB lib, this strictly used string based paths to declare and create the node tree. So, for example, you could have A.B.C and this would create a nodes for each of A, B and C. Within our setter controls, you could reference any node in a path such as "A" or "A.B" or "A.B.C" etc. This was a simple string, nothing fancy and required typing this into the control.

    Nodes were lazily created as data was place inside them. This has some overhead on string processing and memory management. However, what it did do is complete separate logic from the presentation by acting as a simple proxy. As long as no one changed the string reference (often stored as consts in code) then the implementation would not break.

    My only concern with Slash is that if someone changes the name of a bound getter, this will pretty much break the presentation side (i.e uGUI). I suppose we can write proxy objects that are clearly marked as such, but was wondering if there was another approach in Slash DB? (I might also be missing something too)
     
  7. coeing

    coeing

    Joined:
    Mar 17, 2011
    Posts:
    271
    Hi @wdc_bigbluegames,

    Thanks for checking out my library.

    It will indeed break the references without giving a compilation error if someone changes the data context and does not change the string references to it in the data bind scripts (getters, setters,...). But then again the developers should have enough knowledge about the data binding that they know about the role of the data contexts and that they should be kept pretty stable. This is often no problem as you can have arbitrary complex logic behind the data context, the data context is just the interface between the application logic and the UI.

    I think what you mean with proxy objects are already the data contexts that data bind uses, so hopefully this serves your requirements well :)

    Cheers
    Christian
     
  8. coeing

    coeing

    Joined:
    Mar 17, 2011
    Posts:
    271
    @Asse1

    I had time to fix the issue. I was able to reproduce it on my machine (also having `de-de` ;) ) and the fix was straight-forward: Using CultureInfo.InvariantCulture when using Convert.ChangeType in ReflectionUtils.TryConvertValue.

    Just in case you want to fix it locally before I release the next version (which will probably be in a few weeks).
     
  9. coeing

    coeing

    Joined:
    Mar 17, 2011
    Posts:
    271
    The new version 1.23 of Data Bind was just submitted and is now in review :)

    If there are any issues you are still missing, there is a public issue tracker where everybody can add their wishes, bug reports, ideas, feedback,...:

    https://bitbucket.org/coeing/data-bind/issues

    The main additions:
    - #106 Do not store properties in DataBindingPropertyDrawer
    - #105 Convert values in `TryConvertValue` culture-independent

    The new version will be available in a few days in the Unity Asset Store:

    https://assetstore.unity.com/packages/tools/gui/data-bind-for-unity-28301
     
  10. GarethIW

    GarethIW

    Joined:
    Jun 24, 2014
    Posts:
    50
    Hi there! Great product, we've been using it successfully for our UI for a couple of years now.

    I've got an issue regarding GameObjectItemsSetter and a tree-like data context. The structure is a top-level Plan context, which has a collection of Milestone contexts, and Milestone has a collection of Milestones (i.e. a Milestone can have 0 or more sub-milestones and so on down the tree).

    Here's the top-level context:

    Code (CSharp):
    1. public class PlanContext : Context
    2.     {
    3.         private readonly Property<Collection<MilestoneContext>> m_milestonesProperty;
    4.  
    5.         public Collection<MilestoneContext> Milestones
    6.         {
    7.             get => m_milestonesProperty.Value;
    8.             set => m_milestonesProperty.Value = value;
    9.         }
    10.  
    11.         public PlanContext(List<MilestoneContext> milestones)
    12.         {
    13.             m_milestonesProperty = new Property<Collection<MilestoneContext>>(new Collection<MilestoneContext>());
    14.  
    15.             foreach (MilestoneContext milestone in milestones)
    16.             {
    17.                 m_milestonesProperty.Value.Add(milestone);
    18.             }
    19.         }
    20.     }

    And the Milestone context:

    Code (CSharp):
    1. public class MilestoneContext : Context
    2.     {
    3.         private readonly Property<Collection<MilestoneContext>> m_milestonesProperty;
    4.  
    5.         private readonly Property<string> m_nameProperty = new();
    6.         private readonly Property<Color> m_colorProperty = new();
    7.      
    8.         public Collection<MilestoneContext> Milestones
    9.         {
    10.             get => m_milestonesProperty.Value;
    11.             set => m_milestonesProperty.Value = value;
    12.         }
    13.  
    14.         public string Name
    15.         {
    16.             get => m_nameProperty.Value;
    17.             set => m_nameProperty.Value = value;
    18.         }
    19.  
    20.         public Color Color
    21.         {
    22.             get => m_colorProperty.Value;
    23.             set => m_colorProperty.Value = value;
    24.         }
    25.      
    26.  
    27.         public MilestoneContext(string name, Color color, List<MilestoneContext> subMilestones)
    28.         {
    29.             m_milestonesProperty = new Property<Collection<MilestoneContext>>(new Collection<MilestoneContext>());
    30.  
    31.             foreach (MilestoneContext milestone in subMilestones)
    32.             {
    33.                 m_milestonesProperty.Value.Add(milestone);
    34.             }
    35.  
    36.             m_nameProperty.Value = name;
    37.             m_colorProperty.Value = color;
    38.         }
    39. }
    If I attempt to use GameObjectItemsSetter to spawn in the tree, it results in a stack overflow after it gets tuck in a loop repeatedly spawning the first child of a top-level Milestone.

    InsufficientExecutionStackException: Insufficient stack to continue executing the program safely. This can happen from having too many functions on the call stack or function on the stack using too much stack space.
    UnityEngine.Object.Instantiate[T] (T original) (at <bae255e3e08e46f7bc2fbd23dde96338>:0)
    Slash.Unity.DataBind.Foundation.Setters.GameObjectItemsSetter.CreateItem (System.Object itemContext, System.Int32 itemIndex) (at Assets/Scripts/Databind/Scripts/Foundation/Setters/GameObjectItemsSetter.cs:106)
    Slash.Unity.DataBind.Foundation.Setters.ItemsSetter.CreateItems (System.Collections.IEnumerable itemContexts) (at Assets/Scripts/Databind/Scripts/Foundation/Setters/ItemsSetter.cs:143)
    Slash.Unity.DataBind.Foundation.Setters.ItemsSetter.CreateItems (System.Object newValue) (at Assets/Scripts/Databind/Scripts/Foundation/Setters/ItemsSetter.cs:162)
    Slash.Unity.DataBind.Foundation.Setters.ItemsSetter.UpdateTargetValue (UnityEngine.Transform target, System.Object value) (at Assets/Scripts/Databind/Scripts/Foundation/Setters/ItemsSetter.cs:106)
    Slash.Unity.DataBind.Foundation.Setters.ComponentSingleSetter`2[TComponent,TData].OnValueChanged (TData newValue) (at Assets/Scripts/Databind/Scripts/Foundation/Setters/ComponentSingleSetter.cs:133)
    Slash.Unity.DataBind.Foundation.Setters.SingleSetter`1[TData].OnObjectValueChanged () (at Assets/Scripts/Databind/Scripts/Foundation/Setters/SingleSetter.cs:137)
    Slash.Unity.DataBind.Foundation.Setters.SingleSetter`1[TData].Enable () (at Assets/Scripts/Databind/Scripts/Foundation/Setters/SingleSetter.cs:96)
    Slash.Unity.DataBind.Foundation.Setters.ComponentSingleSetter`2[TComponent,TData].Enable () (at Assets/Scripts/Databind/Scripts/Foundation/Setters/ComponentSingleSetter.cs:80)
    Slash.Unity.DataBind.Core.Presentation.DataBindingOperator.OnEnable () (at Assets/Scripts/Databind/Scripts/Core/Presentation/DataBindingOperator.cs:177)
    UnityEngine.GameObject:SetActive(GameObject, Boolean)

    For reference, I'm only creating three contexts: Plan -> Milestone 1 -> Sub milestone 1. You can see in the holder in the screenshot that there's only one child milestone in the tree.



    I may be missing something simple, or it may be that having a tree of data is just out of scope for the items setter!

    This is a cut-down scenario from the actual project I'm working on, but I've reproed in a blank project for you: https://1drv.ms/u/s!AhdchalVsPvXnd4ELBo3kH982J7YQw?e=jWe8Ei (OneDrive link)

    Edit: I've just noticed (looking a the runtime screenshot above) that the Prefab for the Items Setter has changed to Milestone(Clone) - maybe referencing itself(?) rather than the prefab asset that it should be set to. Just checked my Prefab and lo and behold, it's referencing itself. If I change it back to the Milestone asset, save the prefab, then re-open it, it changes back to a self-reference. Odd. Maybe an issue with the inspector, or a unity bug.

    Edit 2: Wow OK, TIL: https://forum.unity.com/threads/prefab-with-reference-to-itself.412240/

    I'll probably have to work around this using a custom ItemsSetter that looks up prefabs from a dictionary SO. I'll leave this here in case future folks come across this problem!
     
    Last edited: Jan 12, 2023
  11. coeing

    coeing

    Joined:
    Mar 17, 2011
    Posts:
    271
    Hi @GarethIW ,

    Thanks a lot for your message and sorry for the late reply.

    Glad you seem to have found the reason for your issue already. Nonetheless I'd like to have a look at your sample when I find time to see if I find a way to prevent the infinite loop inside Data Bind itself. I created an issue at https://bitbucket.org/coeing/data-bind/issues/109/circular-dependency-in-data-context where I will post my updates.
     
    GarethIW likes this.
  12. coeing

    coeing

    Joined:
    Mar 17, 2011
    Posts:
    271
    Hi @GarethIW,

    Just wanted to inform you that I had a look at your sample case. But as you already found out, this is by design in Unity and I don't want to do some special treatment there that differs from the way it is done in Unity. But in the forum thread you posted there seem to be some workarounds, so you have probably fixed it already :)

    Cheers and have a nice weekend
    Christian
     
  13. coeing

    coeing

    Joined:
    Mar 17, 2011
    Posts:
    271
    The new version 1.24 of Data Bind was just submitted and is now in review :)

    If there are any issues you are still missing, there is a public issue tracker where everybody can add their wishes, bug reports, ideas, feedback,...:

    https://bitbucket.org/coeing/data-bind/issues

    The additions:
    - Update to 2022.3.2f1
    - #107 Add flag `NoInitialUpdate` to `ContextDataUpdater` to influence the update order of a two-way binding

    The new version will be available in a few days in the Unity Asset Store:

    https://assetstore.unity.com/packages/tools/gui/data-bind-for-unity-28301
     
  14. Cabron007

    Cabron007

    Joined:
    Feb 12, 2015
    Posts:
    1
    Hi @coeing! Recently started using Data Bind and it has been excellent so far. I believe I just ran into a small bug I wanted to flag, it seems that "double" type values are not rendered correctly on the context holder. When I change the type to float it works but I need the double type for my particular case. It seems it still works as expected in every other area but I cannot tweak double values through the context holder.
     

    Attached Files:

  15. coeing

    coeing

    Joined:
    Mar 17, 2011
    Posts:
    271
    Hi @Cabron007,

    Thanks a lot for using Data Bind and for your report! I will have a look at this issue and if I can reproduce it (which should not be a problem) it will be fixed in the next version :) I created a new issue here: https://bitbucket.org/coeing/data-bind/issues/111/data-property-of-type-double-does-not-show
     
  16. TempusJS

    TempusJS

    Joined:
    May 1, 2019
    Posts:
    1
    Hello, I am having an issue with using this asset, so I am leaving a post. I created a 2D Core project, imported the asset, and performed the same actions as in the following video, but it does not work.


    Device: MacBook Pro 14 M1 Pro
    OS: macOS Sonoma 14.0
    Unity Version: 2022.3.0 LTS
    Asset Version: 1.24

    The Text was initialized to "Hello World" in the constructor of the Property and it was reflected in the View, however, the Text changed through the execution of a method is not reflected in the View.
     
    Last edited: Oct 29, 2023
  17. coeing

    coeing

    Joined:
    Mar 17, 2011
    Posts:
    271
    Hi @Alyxer ,

    Thanks for using my asset. In your video I saw that your data property is called `text`. The default naming convention by which Data Bind looks up the data property behind a property is `${PropertyName}Property`, so in your case `textProperty` or `TextProperty`. It is possible to overwrite the default convention with a DataBindSettings scriptable object, but I guess in your example you are just using the default settings.

    Please also check out the `LabelSetter` example in the `Examples` folder, it is a lot like your example :)

    Hope this helps, if not let me know!

    Cheers
    Christian
     
    TempusJS likes this.
  18. rlatjsdh15

    rlatjsdh15

    Joined:
    Apr 25, 2023
    Posts:
    2
    Hi.
    Your assets are helping me a lot with my UI development.
    I've been using this asset for about a year now, but I'm contacting you because there's an issue that hasn't been fixed.

    The issue is reproduced through the following process.

    1. Create two objects with the same binding component.
    upload_2023-12-3_13-22-4.png
    upload_2023-12-3_13-22-54.png
    upload_2023-12-3_13-23-4.png

    2. Set different context as binding path for two objects.

    3. Select two objects on the hierarchy at the same time.
    upload_2023-12-3_13-24-33.png

    4. You can see that the two differently set paths are set to the same value. (Issue)
    upload_2023-12-3_13-25-19.png

    As this is a well-used asset, I hope it is fixed as soon as possible.

    OS: Windows11
    Unity Version: 2021.3.16 LTS
    Asset Version: 1.24
     
  19. coeing

    coeing

    Joined:
    Mar 17, 2011
    Posts:
    271
    Hi @rlatjsdh15 ,

    Thanks for your kind words and for the detailed reproduction steps. I will have a look at the issue within the next weeks and I hope to find a way to fix it for the next version which should be released in December or January.

    You can follow the issue here: https://bitbucket.org/coeing/data-bind/issues/112/inspector-of-buttonactionsetter-shows-same

    Cheers
    Christian
     
  20. sjlee_unity349

    sjlee_unity349

    Joined:
    Oct 24, 2023
    Posts:
    1
    Hi,

    Is Data binding for Unity thread-safe?

    Leaving aside Flux or MVI, it seems like MVVM pattern in Unity is pretty good for multi-thread programming.
    I'm thinking of doing heavy jobs and update Property<> on the background thread, then View reads Property<> and updates itself on the main-thread.

    I'm wondering if I can use Data Binding for Unity thread-safely in this use case.

    Thanks,
    SJ
     
    Last edited: Dec 6, 2023
  21. coeing

    coeing

    Joined:
    Mar 17, 2011
    Posts:
    271
    Hi @sjlee_unity349,

    Thanks for checking out my asset. Unfortunately I cannot guarantee that it is thread-safe as I did not test it in such an environment yet. It has no built-in functionality to support that, but as the data contexts are normal C# classes it may be possible to implement such support there (e.g. with a thread-safe data context base class).

    If you are testing it, feel free to share your results, sounds like an interesting idea!

    Cheers
    Christian
     
  22. coeing

    coeing

    Joined:
    Mar 17, 2011
    Posts:
    271
    @rlatjsdh15 FYI: I fixed the bug today by disabling multi-object editing for data binding properties and for context path properties. As far as I can see it, there are not many cases where multi-object editing makes sense.

    I hope to release the next version in a few weeks, there are still a few presents missing for Christmas though...
     
  23. rlatjsdh15

    rlatjsdh15

    Joined:
    Apr 25, 2023
    Posts:
    2
    @coeing Thank you for your help.
    Now be able to select two text objects in the editor and change their font and size. :)
     
  24. coeing

    coeing

    Joined:
    Mar 17, 2011
    Posts:
    271
    The new version 1.25 of Data Bind was just submitted and is now in review :)

    If there are any issues you are still missing, there is a public issue tracker where everybody can add their wishes, bug reports, ideas, feedback,...:

    https://bitbucket.org/coeing/data-bind/issues

    Here is the full changelog:
    Version 1.25 (22/12/2023)
    *************************

    * #112 Do not allow multi-object editing for data bindings and context paths

    * #111 Add inspector for double fields in data contexts

    The new version will be available in a few days in the Unity Asset Store:

    https://assetstore.unity.com/packages/tools/gui/data-bind-for-unity-28301