Data Binding For Unity: The Package that helps you with the easy way of binding

Overview

One-Way MVVM style data binding for unity. Simple data binding system which enables one-way data-binding from observable model data to view model components.

You all are aware of the term MVVM, right? If not then don't worry I am here to help you. First The MVVM is called a Model View View Model structural design pattern. It also helps with binding the data. MVVM pattern is most used in developing apps and websites but what about games? I have created a small package that you can use in your project. It helps a lot when your project has more UI screens and also when they are dependent on the API callbacks.

Topics

  1. Introduction to the MVVM pattern
  2. How to import the package
  3. Getting Started
  4. Usage

1. Introduction to the MVVM pattern

Models: It contains data, for example, classes and structs.

Views: Show visual elements and controls on the UI, They are mainly subclasses of MainViews.

View Models: They provide channels that transform model information into values that can be displayed on views. They are usually classes.



This pattern is also familiar with Model View Controller. But In MVVM the model view controller hides that role.

2. How to import the package


 You can also import the samples from the package manager window

Note: In this Package also uses the TextMeshPro package so make sure You have to download the package from the package manager in unity or you can delete the Example folder found In the DataBind folder in Project.

3. Getting Started

 Explore the MyExample scene in the Sample folder.

◆ The scene contains a View Model game object with a ViewModel component Ans also ViewModel scriptable object with ViewModelScriptable component changes the values on the component to observe changes in the scene.

 The scene contains a few sample bindings on single-game objects as well as a slightly more complex list example that binds prefab instances to a list of configuration data.

3. Usage

◆ Define a new class that extends ViewModel or ViewModelScriptable. ViewModels are unity mono behaviors and ViewModelScriptable are unity scriptable objects which should contain serval DataSource or DataSourceList fields.

Example:

You can create both the MonoBehavior and ScriptableObject ViewModels.

1. MonoBehavior Example:
public class ItemViewModel: ViewModel
{
public SpriteDataSource ItemImage;
public StringDataSource Itemname;
public IntDataSource itemPrice;
}

2. ScriptableObject Example:
public class ItemViewModel : ViewModelScriptable
{
  public SpriteDataSource ItemImage;
  public StringDataSource Itemname;
public IntDataSource itemPrice;
}

//At runtime, data source values can be accessed via the Value property.

ViewModel viewModel = gameObject.GetComponent<ItemViewModel>();
viewModel.ItemImage.Value = someSprite;
viewModel.Itemname.Value = “Chocolate”;
viewModel.itemPrice.Value = 55;

Magic One 😎:


Magic One Loading...


Magic Two 😎:


Magic Two Loading...


Magic Three😎:


Magic Three Loading...

Conclusion

I hope this blog proved to be useful!! At least up to an extent where the user understands its functioning and its purpose.

Try for yourself. You can show your creativity too on any medium. At last, just wanna say that if you liked the blog & its content, do share it among your network.


Comments