百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
U

Unidux

> 编程语言
开源

适用于 Unity 的 Redux 架构

393 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

适用于 Unity 的 Redux 架构

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 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

C#reduxunity3d

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

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