Giter Site home page Giter Site logo

unidux's Introduction

Unidux is practical application architecture for Unity3D UI.

It's inspired by Redux.

Install

Import unitypackage from latest releases.

Usage

  1. Create your Unidux singleton and place it to unity scene.
using Unidux;
public class Unidux : SingletonMonoBehaviour<Unidux>
{
    private Store<State> _store;
    public Store<State> Store
    {
        get
        {
            if (null == _store)
            {
                _store = new Store<State>(new State());
                // Add reducers in this place.
                _store.AddReducer<CountAction>(CountReducer.Reduce);
            }
            return _store;
        }
    }

    void Update()
    {
        this.Store.Update();
    }
}
  1. Create state class to store application state.
using Unidux;
public class State : StateBase<State>
{
    public int Count { get; set; }
}
  1. Define action to change state.
public enum CountAction
{
    Increment,
    Decrement
}
  1. Create reducer to update state
using Unidux;

public static class CountReducer
{
    public static State Reduce(State state, CountAction action)
    {
        switch (action)
        {
            case CountAction.Increment:
                state.Count++;
                break;
            case CountAction.Decrement:
                state.Count--;
                break;
        }

        return state;
    }
}
  1. Create Renderer to display state and attach it to Text GameObject.
using UnityEngine;
using UnityEngine.UI;

[RequireComponent(typeof(Text))]
public class CountRenderer : MonoBehaviour
{
    void OnEnable()
    {
        var text = this.GetComponent<Text>();
        var store = Unidux.Instance.Store;
        this.AddDisableTo(store, state => text.text = state.Count.ToString());
    }
}
  1. Create dispatcher to update count and attach it to GameObject.
[RequireComponent(typeof(Button))]
public class CountDispatcher : MonoBehaviour
{
    public CountAction ActionType = CountAction.Increment;

    void Start()
    {
        var button = this.GetComponent<Button>();
        button.onClick.AddListener(() => Unidux.Instance.Store.Dispatch(ActionType));
    }
}

That's it!

Example

License

MIT

unidux's People

Contributors

mattak avatar pine 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.