https://github.com/mminer/reduxity
State management for Unity inspired by Redux.
https://github.com/mminer/reduxity
redux unity unity3d
Last synced: 5 months ago
JSON representation
State management for Unity inspired by Redux.
- Host: GitHub
- URL: https://github.com/mminer/reduxity
- Owner: mminer
- License: mit
- Created: 2022-07-28T00:24:49.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2022-07-28T00:29:49.000Z (almost 3 years ago)
- Last Synced: 2024-04-14T22:17:41.462Z (about 1 year ago)
- Topics: redux, unity, unity3d
- Language: C#
- Homepage:
- Size: 3.91 KB
- Stars: 4
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Reduxity
Redux + Unity. Provides a bare-bones state management container inspired by
[Redux](https://redux.js.org).Redux is heavily used in web development. Is this pattern a good fit for game
state? Probably not in most cases, but the experiment is ongoing.## Using
Define a state object:
```csharp
struct GameState
{
public Vector3Int playerPosition;
}
```Add actions to affect the state:
```csharp
interface IGameAction {}struct MoveAction : IGameAction
{
public Vector3Int direction;
}
```Create a reducer to update the state when an action is dispatched:
```csharp
static GameState Reducer(GameState state, IGameAction action)
{
switch (action)
{
case MoveAction moveAction:
state.playerPosition += moveAction.direction;
return state;default:
return state;
}
}
```Tie it all together with a store:
```csharp
Store store = new(Reducer);
```Dispatch changes:
```csharp
store.Dispatch(new MoveAction { direction = Vector3Int.up });
```Subscribe to changes:
```csharp
store.changed += _ => Debug.Log($"New position: {store.state.playerPosition}");
```