Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
U

Unidux

> 编程语言
Open source

Redux Architecture for Unity

393 stars0 likes2 views
WebsiteGitHub

About

Redux Architecture for Unity

Unidux is practical application architecture for Unity3D UI.

It's inspired by Redux.

Install

UPM

Add following two lines to Pacakges/manifest.json.

{
 "com.neuecc.unirx": "https://github.com/neuecc/UniRx.git?path=Assets/Plugins/UniRx/Scripts",
 "me.mattak.unidux": "https://github.com/mattak/Unidux.git?path=Assets/Plugins/Unidux/Scripts",
 // ...
}

Unity package

No longer supported. If you need older versions, import unitypackage from latest releases.

Usage

  1. Create your Unidux singleton and place it to unity scene.
…

Note: ReplaySubject is a ReactiveX concept provided by UniRx in this example.

  1. Create state class to store application state.
csharp
using System;

[Serializable]
public class State : StateBase
{
    public int Count = 0;
}
  1. Define action to change state. Define Reducer to move state.
…
  1. Create Renderer to display state and attach it to Text GameObject.
csharp
[RequireComponent(typeof(Text))]
public class CountRenderer : MonoBehaviour
{
    void OnEnable()
    {
        var text = this.GetComponent();

        Unidux.Subject
            .TakeUntilDisable(this)
            .StartWith(Unidux.State)
            .Subscribe(state => text.text = state.Count.ToString())
            .AddTo(this)
            ;
    }
}
  1. Create dispatcher to update count and attach it to GameObject.
csharp
[RequireComponent(typeof(Button))]
public class CountDispatcher : MonoBehaviour
{
    public Count.Action Action = Count.ActionCreator.Increment();

    void Start()
    {
        this.GetComponent()
            .OnClickAsObservable()
            .Subscribe(state => Unidux.Store.Dispatch(Action))
            .AddTo(this)
            ;
    }
}

That's it!

Example

  • Counter
  • List
  • Todo
  • Middlewares
  • SimpleHttp

Dependencies

  • UniRx
  • MiniJSON (for Unidux.Experimental.Editor.StateJsonEditor)

API

StateBase

csharp
public class State : StateBase
{
    public int Count;
}

.Clone()

csharp
State _state = new State();
State _clonedState = _state.Clone();

Create a deep clone of the current state. Useful for Immutability.

Store

csharp
IReducer[] reducers = new IReducer[]{};
Store _store = new Store(State, reducers);
// State must extend StateBase

.State

Get the state as passed to the constructor.

.Dispatch(object)

Dispatch an event of TAction object, which will trigger a Reducer.

.ApplyMiddlewares(params Middleware[] middlewares)

Apply middlewares to Store object which implement delegate function of Middleware.

.Update()

When at least one reducer has been executed, trigger all the renderers with a copy of the current state.

.ForceUpdate()

Trigger all registered renderers with a copy of the current state regardless of any reducers having been executed.

SingletonMonoBehaviour

csharp
public class Foo : SingletonMonoBehaviour {}

A singleton base class to extend. Extends MonoBehaviour.

.Instance

csharp
public class Foo : SingletonMonoBehaviour {}

Foo.Instance

The instance of the base class.

Performance

Clone()

Default implemention of StateBase.Clone() is not fast, because it uses BinaryFormatter & MemoryStream. And Unidux creates new State on every State chaning (it affects a few milliseconds). So in case of requiring performance, override clone method with your own logic.

e.g.

csharp
[Serializable]
class State : StateBase
{
    public override object Clone()
    {
        // implement your custom deep clone code
    }
}

Equals()

Default implemention of StateBase.Equals() and StateElement.Equals() is not fast, because it uses fields and properties reflection. In case of edit state on UniduxPanel's StateEditor, it calls Equals() in order to set IsStateChanged flags automatically. So in case of requiring performance, override Equals() method with your own logic.

e.g.

csharp
[Serializable]
class State : StateBase
{
    public override bool Equals(object obj)
    {
        // implement your custom equality check code
    }
}

Thanks

  • @austinmao for suggestion of Ducks and UniRx.
  • @pine for description improvement.
  • @jesstelford for fix document.
  • @tenmihi for fix document.
  • @kn1cht for fix .net 4.0 runtime error.
  • @shiena for upm support.

License

MIT

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

C#reduxunity3d

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言