Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Data Binding in Unity

Discussion in '2017.1 Beta' started by Cherubim79, May 17, 2017.

  1. Cherubim79

    Cherubim79

    Joined:
    May 3, 2014
    Posts:
    56
    Hey guys,

    I'm wanting to build some charting controls in Unity. I started on a small project (see pics below) to do so back around 2014 when Unity 4.3 was around before Unity UI even came to be (laughingly I came across some overlap in things I was already doing when 4.6 came out), but I ran into lots of problems because Unity at the time was very limited on the topic of creating controls and data binding to them like you could do in traditional .NET Winforms, WPF, or UWP XAML. I'd love to make a reusable set of controls that you could plug into your Game/App for data visualization, and now that this new beta is using .NET 4.6 I'm revisiting these old ideas just to get a feel for the lay of the land in the current tech.

    What would be great is if I could set a DataContext property on a script, and bind properties like you would in XAML (MVVM would be even better), or if I could at least set an ItemSource property to some collection of objects with a DisplayPath and the like, then have that property automatically display, even if I set the dataset initially at design time in the inspector to some arbitrary collection. Maybe a system where setting data related events calls some arbitrary callback method that rebuilds that area of the display on the fly (like a chart, or a grid, etc).

    The process I did below was very manual, I basically created a run-time instantiated method that would try to update everything on the fly on a background worker. See VirtualUIAreaChart at http://virtualui.codeplex.com/SourceControl/latest#Assets/Scripts/UI/VirtualUIAreaChart.cs and also the DataLoader, Repeater, and RepeaterItem controls I tried to make.

    Eventually it got a bit messy to maintain, especially when we start talking about dynamic columns in a repeater / data grid control. I got it technically working, but I just felt that it wasn't very clean and maintainable, and frankly, I was worn out and put the project on the backburner as there's been lots of other projects I've been involved in with Xamarin, IoT, networking, home automation, etc. The one biggest kicker to making this harder to maintain was dealing with public fields only showing in the Inspector versus properties, that's a big one. I need a change on the Inspector to run setters on properties to do some of the legwork for me, that would make things a lot cleaner.

    At least the pics look pretty. :p

    Just wondering if the data binding situation has changed any in Unity in this new beta, and what are the best practices as far as data loading?



     
  2. Jaimi

    Jaimi

    Joined:
    Jan 10, 2009
    Posts:
    6,208
    Cherubim79 likes this.
  3. Cherubim79

    Cherubim79

    Joined:
    May 3, 2014
    Posts:
    56
    Love it! Thank you! Now comes the question of what are the pros and cons of both frameworks in comparison to each other. It would be great if there were one unified standard for MVVM in Unity3D, sometimes having options can lead to trade offs among alternatives. I'll check these out and see which one fits the bill for my needs. Thank you.
     
  4. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    Even WPF doesn't have a unified standard for MVVM :D