RxSateMachine v2.0 is out! It is more simpler and easy to use! for old version look in to Old folder.
State machine makes managing states easy, it is widely used in many games and apps. However, there are not many state machines that designed with front-end in mind.
Often, integrating multiple states with UI transition can be a huge hassle and needs extra code to make simple transitions working with states. RxStateMachine is designed with "front end first" and "Reactive" in mind.
Thanks to neuecc for Reactive Extensions for Unity(unirx)!
Unity +2018.2, UniRx, Reactive extension for Unity. latest source code(as of 02/13/18) already included in this repository, but if you want to get a newer vision, Download Unirx
- Regular
- (ST1)Enter -> (ST1)Exit -> (ST2)Enter ->
- Blend
- (ST1)Enter -> (ST1,ST2)Exit,Enter -> (ST2,ST3)Exit,Enter ->
- Subscribe OnEnter/Exit of State or just Sate changes
- Expend OnEnter/Exit behaviour as you wanted
- Support multiple state machine instances
- State Transition can be canceled
- no co-routine used, more reliable functionality
To use the state machine you need a few simple steps
using using _Scripts.RxDevKit.StateMachine;
public enum MyState
{
Init,State01,State02,State03
}
private StateMachine<MyState> _stateMachine;
_stateMachine.ChangeState(MyState.Init);
var mainCam = Camera.main;
_stateMachine.Subscribe(state =>
{
Debug.Log($"you are in {state}");
switch (state)
{
case MyState.State01:
mainCam.backgroundColor = Color.gray;
break;
case MyState.State02:
mainCam.backgroundColor = Color.magenta;
break;
case MyState.State03:
mainCam.backgroundColor = Color.white;
break;
default:
mainCam.backgroundColor = Color.black;
break;
}
});
with Rx SateMachine, you can subscribe Sate's Enter and Exit. There are some points you need to pay attention to avoid confusion.
- On Enter, On Exit only accepts
Func<IObservable<Unit>>
data type.
_stateMachine.OnEnter(MyState.Init, () =>Observable.Timer(TimeSpan.FromSeconds(2f)).AsUnitObservable());
- you can subscribe this OnEnter/OnExit stream as many as you can.
- However, OnEnter/OnExit transition will be completed when the last(longest) steam is complete.
- Which means, you can subscribe many OnEnter/OnExit on a State to handle different things.
- below example using RxFadeManager to fade in and out on State changes.
namespace Demo.StateMachine
{
public class Demo2State : MonoBehaviour
{
public CanvasGroup Panel;
public MyState ThisState;
private StateMachine<MyState> _stateMachine;
void Start ()
{
_stateMachine = Demo02Manager.Instance.StateMachine;
var duration = Demo02Manager.Instance.FadeDuration;
Panel.DisableCanvasGroup(true);
_stateMachine.OnEnter(ThisState, () => Panel.FadeIn(duration).AsUnitObservable());
_stateMachine.OnExit(ThisState, () => Panel.FadeOut(duration).AsUnitObservable());
if(ThisState == MyState.Init) _stateMachine.ChangeState(MyState.Init);
}
}
}
To use RxStateMachine over multiple scripts is also simple. Here's basic example how to setup RxStateMachine in multiple scripts
in this example, we declare StateMachine in GameManager.cs and make GameManager as a singleton to call it from other scripts
<GameManager.cs>
public class GameManager : MonoBehaviour
{
}
<RedState.cs>
public class RedState : MonoBehaviour
{
}
<BlueState.cs>
public class BlueState : MonoBehaviour
{
}
[TODO] add description with examples...
[TODO] add a description with examples...