MVVM and DataBinding in Unity

Hi folks.

I’m currently setting up a MVVM system by myself, but I’m rather unsure if I’m on the right track, so I post here some code just to hear if you guys could tell me if it’s the right direction or what I can do better.

Mostly the DataBinding part is something I think I haven’t got into yet so here’s what I have till now:

ViewModel class

public class ViewModel : MonoBehaviour
{
    
    private DataObj _data;
    internal DataObj Data
    {
        get { return _data; }
        set
        {
            _data = value;
            if (DataChanged != null)
                DataChanged(_data);
        }
    }

    internal event Action<DataObj> DataChanged;
    

    private void Start()
    {
        Data.PropertyChanged += Data_PropertyChanged;
    }

    private void OnDestroy()
    {
        Data.PropertyChanged -= Data_PropertyChanged;
    }

    void Data_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        Data = (DataObj)sender;
    }
}

View class

public class View : MonoBehaviour
{
    [SerializeField] private ViewModel viewModel;

    [SerializeField] private Sprite sprite;
    [SerializeField] private TextMesh text;

    private void Awake()
    {
        // play slide in animation
        viewModel.DataChanged += viewModel_DataChanged;
    }

    private void OnDestroy()
    {
        viewModel.DataChanged -= viewModel_DataChanged;
    }

    void viewModel_itemDataChanged(DataObj obj)
    {
        UpdateUI(obj);
    }

    void UpdateUI(DataObj obj)
    {
        sprite = new Sprite(obj.spriteName);
        text = obj.text;
    }
}

As you see my ViewModel class is handling the communication to the data model and will throw an event when new data is arriving. The View is listening and updating all UI elements it knows.

Now here’s the first part I’m unsure about: Is it actually good that I have this (at the moment) small view class, which knows all UI elements in my scene and updates them or is it better to have several smaller view class, e.g. SpriteView, TextView and these all share the same viewmodel?

Second question is about the data binding. For now this is only a one way data binding, the logic gets new data, throws event and view will update. But how do I a two way binding? My first approch was to add to my view class:

void UpdateText(string newText)
{
    viewModel.Data.text = newText;
    UpdateUI(viewModel.Data);
}

but I’m not feeling good about that. Though it works, it doesn’t seem to me like the right way to do it in that pattern.

So I hope you guys could give me some comments about it and maybe what I should improve.

Many thanks,

pahe

PS: I know there is something like NData or EZData, but either plugin will need the UI framework too, which is over my budget. Additionally I would like to understand the full pattern first before buying a plugin which does what I want to understand.

The main reason for having a View and ViewModel is for separating UI design from application logic. As for data-binding, the way I’ve implemented it is to have the view-model keep track of bound fields that are set/changed/propagated through a generic SetValue() method.

I’ve been longing for strong MVVM support in Unity so badly, I ended up building a framework from scratch. It’s built upon the unity 4.6 UI system so no extra UI framework needed. It allows you to create views using XML, it supports theming, animations, etc. If you’ve used other MVVM/MVC/MVP frameworks such as WPF/Silverlight (XAML), Caliburn, AngularJS etc. it should be familiar. It’s available here: www.markux.com.

Thanks exmakina for your response exmakina.
The question is some years old now and I have some experience now with the pattern. Tough it doesn’t fit always the needs of my projects, I learned a lot of seperating game logic from view logic (which is always a good thing and I would always recommend it, if the project exceeds the size of a one day project).

I had a quick look at your link and it looks pretty nice. Maybe I’ll give it a try again.