Coder Social home page Coder Social logo

unity-basicbind's Introduction

Basic Bind (for Unity)

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<ProfileViewModel>();
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<string>(out var value))
        {
            if (TextField) TextField.text = value;
        }
    }
}

unity-basicbind's People

Contributors

benkeatingsmith avatar chippolot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.