适用于 Unity 的 Redux 架构
Unidux is practical application architecture for Unity3D UI.
It's inspired by Redux.
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",
// ...
}No longer supported. If you need older versions, import unitypackage from latest releases.
…Note: ReplaySubject is a ReactiveX concept
provided by UniRx in this example.
using System;
[Serializable]
public class State : StateBase
{
public int Count = 0;
}…[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)
;
}
}[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!
StateBasepublic class State : StateBase
{
public int Count;
}.Clone()State _state = new State();
State _clonedState = _state.Clone();Create a deep clone of the current state. Useful for Immutability.
StoreIReducer[] reducers = new IReducer[]{};
Store _store = new Store(State, reducers);
// State must extend StateBase.StateGet 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.
SingletonMonoBehaviourpublic class Foo : SingletonMonoBehaviour {}A singleton base class to extend.
Extends MonoBehaviour.
.Instancepublic class Foo : SingletonMonoBehaviour {}
Foo.InstanceThe instance of the base class.
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.
[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.
[Serializable]
class State : StateBase
{
public override bool Equals(object obj)
{
// implement your custom equality check code
}
}暂无开放 Issues,或尚未同步最近议题。