Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/benkeatingsmith/Unity-BasicBind

Simple one-way MVVM style data binding for Unity.
https://github.com/benkeatingsmith/Unity-BasicBind

databinding mvvm one-way-binding ui unity

Last synced: 3 months ago
JSON representation

Simple one-way MVVM style data binding for Unity.

Awesome Lists containing this project

README

        

# Basic Bind (for Unity)

![](example.gif)

*One-way MVVM style data-binding for Unity.*

Basic Bind is a simple data binding system which enables one-way data binding from observable model data to view components.

## Installation
### Manual Import
1. Clone or dowload this repository somewhere in your Unity project's `Assets` folder.

## Getting Started
* Explore the example scene in the `Example` folder.
* The scene contains a View Model game object with a ViewModel component. Change the values on the component to observe changes in the scnee.
* The scene contains a few sample bindings on single game objects as well as a slightly more complex list example which binds prefab instances to a list of configuration data.

## Usage
To get started data-binding, first define a new class which extends `ViewModel`.
ViewModels are Unity MonoBehaviours which should contain several `DataSource` or `DataSourceList` fields.

Example:
```
public class ProfileViewModel : ViewModel
{
public StringDataSource Username;
public SpriteDataSource Avatar;
}
```

At runtime, data source values can be accessed via the `Value` property.
```
var viewModel = gameObject.GetComponent();
viewModel.Username.Value = "Ben";
viewModel.Avatar.Value = someSprite;
```

To create a binding between a component and a ViewModel, create a new `DataBinding` component.
DataBinding should contain `DataSourceReference` fields which allow for the binding and serialization of data-binding links.
DataBinding classes use the `Setup` method to create bindings.
You can use the `[AllowedDataTypes]` attribute to help the inspector filter a ViewModel's data sources by type.

Example:
```
public class TextFieldBinding : DataBinding
{
public Text TextField;
[AllowedDataTypes(typeof(string))] public DataSourceReference StringSource;

protected override void Setup()
{
Bind(StringSource, OnValueChanged);
}

private void OnValueChanged(object sender, EventArgs eventArgs)
{
if (StringSource != null && StringSource.TryGetValue(out var value))
{
if (TextField) TextField.text = value;
}
}
}
```