Hi there!
The first version of Data Bind for Unity is available in the Asset Store! Check it out now for a very low initial price and help me with your feedback to further develop it for your needs!
Introduction:
A clean separation between logic/data and visualization is a must-have, not only in software projects (Separation of content and presentation - Wikipedia). This guarantees that the business logic of the application isn’t dependent on the presentation, so the visualization can be easily adapted.
One famous architecture, at least in the Microsoft universe, is the MVVM pattern (Model–view–viewmodel - Wikipedia) which is highly utilized in Windows Presentation Foundation (WPF, Windows Presentation Foundation - Wikipedia). I don’t want to get too much into detail of this framework as it a fully developed business framework with a huge amount of complex stuff.
The part we are interested in is the data binding between the view and the view model. The view model is an additional structure which sits between the view and the logic. It gets its data from the logic of the application and provides an interface for the view to use. Via data bindings the view can use the data provided by the view model to show it to the user.
This is what we adapted with our plugin, so you’ll have a common way how your UI is separated from you game logic to get many cool advantages:
- Don’t be afraid that UI changes break your game any more, the logic is completely separated
- Exchange your UI system without touching any logic (e.g. from NGUI to Unity UI)
- Easily extend the plugin to use it with a custom UI, world objects or custom input commands
Example: Easy Health Bars
Tired of remembering to update two variables (actor.Health = 20.0f, healthLabel.Text = „20“) if the health of your character changed, so the UI is updated correctly?
No problem, Data Bind will take care of this from now on. You just update the data in your context and the UI will update magically!
Okay, not really magically, but without any work on your side. In principal it works like this:
- The data variables in your context are wrapped in a so called “Data property”
- The bindings on UI side register for value changes on this properties
- When your code sets a new value for your data, the property informs its listeners, so they can update the UI properly.
In your code…
public class PlayerContext : Context
{
#region Fields
/// <summary>
/// Data binding property, used to get informed when a data change happened.
/// </summary>
private readonly Property<int> healthProperty = new Property<int>();
#endregion
#region Properties
public int Health
{
get
{
return this.healthProperty.Value;
}
set
{
this.healthProperty.Value = value;
}
}
#endregion
}
…at the same time on the UI side:
More information
Check out the official documentation with tutorials, examples, API and explanations. If any questions are left, just send me a message or post in this thread!
Hope our asset serves you as well as it does for our projects to keep your logic and presentation separated from each other! A big update with many additional bindings was already created and is reviewed currently. I’ll keep you updated when it arrives.