{"id":13692654,"url":"https://github.com/gilpark/RxStateMachine","last_synced_at":"2025-05-02T19:32:27.603Z","repository":{"id":79110464,"uuid":"120951484","full_name":"gilpark/RxStateMachine","owner":"gilpark","description":"Reacitve StateMachine for Unity 3D","archived":false,"fork":false,"pushed_at":"2018-11-12T23:21:23.000Z","size":3827,"stargazers_count":25,"open_issues_count":1,"forks_count":4,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-04T11:01:50.875Z","etag":null,"topics":["reactive","reactive-extension","reactive-streams","state-machine","unirx","unity","unity3d"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gilpark.png","metadata":{"files":{"readme":"ReadMe.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2018-02-09T20:19:27.000Z","updated_at":"2023-10-30T02:02:33.000Z","dependencies_parsed_at":"2023-06-01T11:46:40.679Z","dependency_job_id":null,"html_url":"https://github.com/gilpark/RxStateMachine","commit_stats":null,"previous_names":["sendtogil/rxstatemachine"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gilpark%2FRxStateMachine","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gilpark%2FRxStateMachine/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gilpark%2FRxStateMachine/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gilpark%2FRxStateMachine/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gilpark","download_url":"https://codeload.github.com/gilpark/RxStateMachine/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252095407,"owners_count":21693911,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["reactive","reactive-extension","reactive-streams","state-machine","unirx","unity","unity3d"],"created_at":"2024-08-02T17:01:00.612Z","updated_at":"2025-05-02T19:32:26.552Z","avatar_url":"https://github.com/gilpark.png","language":"C#","funding_links":[],"categories":["Structure"],"sub_categories":[],"readme":"# Unity3D - RxStateMachine\n![](Images/Cancelable.gif)\n\nRxSateMachine v2.0 is out! It is more simpler and easy to use! for old version look in to [Old](https://github.com/sendtogil/RxStateMachine/tree/master/Old) folder.\n\nState machine makes managing states easy, it is widely used in many games and apps.\nHowever, there are not many state machines that designed with front-end in mind.\n\nOften, integrating multiple states with UI transition can be a huge hassle and needs extra code to make simple transitions working with states.\nRxStateMachine is designed with \"front end first\" and \"Reactive\" in mind.\n\nThanks to [neuecc](https://github.com/neuecc) for Reactive Extensions for Unity(unirx)!\n\n##### Dependencies\n\nUnity +2018.2, UniRx, Reactive extension for Unity. latest source code(as of 02/13/18) already included in this repository, \nbut if you want to get a newer vision, **[Download Unirx](https://assetstore.unity.com/packages/tools/unirx-reactive-extensions-for-unity-17276)**\n\n## Features\n\n#### 2 Transition Modes\n* Regular\n    * (ST1)Enter -\u003e (ST1)Exit -\u003e (ST2)Enter -\u003e \n* Blend\n    * (ST1)Enter -\u003e (ST1,ST2)Exit,Enter -\u003e (ST2,ST3)Exit,Enter -\u003e\n\n#### Flexible\n* Subscribe OnEnter/Exit of State or just Sate changes \n* Expend OnEnter/Exit behaviour as you wanted\n* Support multiple state machine instances\n\n#### Responsive\n* State Transition can be canceled\n* no co-routine used, more reliable functionality\n\n## Simple Usage (State change pub/sub)\nTo use the state machine you need a few simple steps\n\n##### 1. Include the RxStateMachine package\n\n```C#\nusing using _Scripts.RxDevKit.StateMachine;\n\n```\n\n##### 2. Define your states using an Enum \n\n```C#\npublic enum MyState\n{\n    Init,State01,State02,State03\n}\n```\n##### 3. Create a variable to store a reference to the State Machine \n\n```C#\nprivate StateMachine\u003cMyState\u003e _stateMachine;\n```\n\n##### 4. You are now ready to change(publish) state by simply calling `ChangeState()`\n```C#\n_stateMachine.ChangeState(MyState.Init);\n```\n\n##### 5. Subscribe StateChanges!\n```C#\n    var mainCam = Camera.main;\n\t_stateMachine.Subscribe(state =\u003e\n\t\t\t{\n\t\t\t\tDebug.Log($\"you are in {state}\");\n\t\t\t\tswitch (state)\n\t\t\t\t{\n\t\t\t\t\tcase MyState.State01:\n\t\t\t\t\t\tmainCam.backgroundColor = Color.gray;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase MyState.State02:\n\t\t\t\t\t\tmainCam.backgroundColor = Color.magenta;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase MyState.State03:\n\t\t\t\t\t\tmainCam.backgroundColor = Color.white;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tdefault:\n\t\t\t\t\t\tmainCam.backgroundColor = Color.black;\n\t\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t});\n```\n## Basic Usage (OnEnter/OnExit Async Subscription)\nwith Rx SateMachine, you can subscribe Sate's Enter and Exit.\nThere are some points you need to pay attention to avoid confusion.\n1. On Enter, On Exit only accepts `Func\u003cIObservable\u003cUnit\u003e\u003e` data type.\n```C#\n_stateMachine.OnEnter(MyState.Init, () =\u003eObservable.Timer(TimeSpan.FromSeconds(2f)).AsUnitObservable());\n```\n2. you can subscribe this OnEnter/OnExit stream as many as you can.\n3. However, OnEnter/OnExit transition will be completed when the last(longest) steam is complete.\n4. Which means, you can subscribe many OnEnter/OnExit on a State to handle different things.\n\n* below example using [RxFadeManager](https://github.com/sendtogil/RxFadeManager) to fade in and out on State changes.\n```C#\nnamespace Demo.StateMachine\n{\n\tpublic class Demo2State : MonoBehaviour\n\t{\n\t\tpublic CanvasGroup Panel;\n\t\tpublic MyState ThisState;\n\t\tprivate StateMachine\u003cMyState\u003e _stateMachine;\n\t\n\t\tvoid Start ()\n\t\t{\n\t\t\t_stateMachine = Demo02Manager.Instance.StateMachine;\n\t\t\tvar duration = Demo02Manager.Instance.FadeDuration;\n\t\t\tPanel.DisableCanvasGroup(true);\n\t\t\t\n\t\t\t_stateMachine.OnEnter(ThisState, () =\u003e Panel.FadeIn(duration).AsUnitObservable());\n\t\t\t_stateMachine.OnExit(ThisState, () =\u003e Panel.FadeOut(duration).AsUnitObservable());\n\t\t\n\t\t\tif(ThisState == MyState.Init) _stateMachine.ChangeState(MyState.Init);\n\t\t}\n\t}\n}\n\n```\n\n## Advanced Usage\n#### Multiple Script Setup\nTo use RxStateMachine over multiple scripts is also simple.\nHere's basic example how to setup RxStateMachine in multiple scripts\n\n\n\n\n##### 1. Declare StateMachine in the desired script\nin this example, we declare StateMachine in *GameManager.cs* and make GameManager as a singleton to call it from other scripts\n\n```C#\n\u003cGameManager.cs\u003e\n    public class GameManager : MonoBehaviour\n    {\n      \n    }\n```\n\n##### 2. Add State Script Component to subscribe state changes with callbacks \n\n```C#\n\u003cRedState.cs\u003e\n    public class RedState : MonoBehaviour\n    {\n       \n    }\n    \n\u003cBlueState.cs\u003e    \n    public class BlueState : MonoBehaviour\n    {\n       \n    }\n```\n\n#### Manual Transition\n[TODO] add description with examples...\n\n### Cancel Invocation Order \u0026 Usage\n[TODO] add a description with examples...\n \n#### [TODO] Example Usage\n\n## Exmaples\n#### Multiple StateMachine Example\n#### StateMachine Example\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgilpark%2FRxStateMachine","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgilpark%2FRxStateMachine","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgilpark%2FRxStateMachine/lists"}