https://github.com/uzimaru0000/unitea
Implementation of The Elm Architecture for Unity3D
https://github.com/uzimaru0000/unitea
cshape elm-architecture framework state-management unity3d
Last synced: about 1 year ago
JSON representation
Implementation of The Elm Architecture for Unity3D
- Host: GitHub
- URL: https://github.com/uzimaru0000/unitea
- Owner: uzimaru0000
- License: mit
- Created: 2019-07-02T04:09:40.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-06-12T22:30:00.000Z (almost 6 years ago)
- Last Synced: 2025-02-28T01:47:46.874Z (about 1 year ago)
- Topics: cshape, elm-architecture, framework, state-management, unity3d
- Language: C#
- Homepage:
- Size: 1.55 MB
- Stars: 32
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 
UniTEA is an implementation of The Elm Architecture for Unity3D.
## Environment
- Unity3D 2018.4 or higher
- C# 7.0
## Install
Add line in Packages/manifest.json
```json
{
"dependencies" : {
...
"com.uzimaru0000.unitea": "https://github.com/uzimaru0000/UniTEA.git",
...
}
}
```
## Usage
1. Create your application Model. You should use struct.
```c#
public struct Model
{
public int count;
}
```
2. Create a message for your application. You should use enum.
```c#
public enum Msg
{
Increase,
Decrease
}
```
3. Create a class that wraps the message. Implement the `IMessenger` interface.
```c#
using UniTEA;
public class IncreaseMsg : IMessenger
{
public Msg GetMessage() => Msg.Increase;
}
public class DecreaseMsg : IMessenger
{
public Msg GetMessage() => Msg.Decrease;
}
```
4. Create Updater class. Implement the `IUpdater` interface.
```c#
using UniTEA;
public class Updater : IUpdater
{
public (Model, Cmd) Update(IMessenger msg, Model model)
{
switch (msg)
{
case IncreaseMsg _:
return (new Model
{
count = model.count + 1;
}, Cmd.none);
case DecreaseMsg _:
return (new Model
{
count = model.count - 1;
}, Cmd.none);
default:
return (model, Cmd.none);
}
}
}
```
5. Create Renderer class. Implement the `IRenderer` interface.
```c#
using UnityEngine;
using UnityEngine.UI;
using UniTEA;
public class Renderer : MonoBehaviour, IRenderer
{
[SerializeField]
Text display;
[SerializeField]
Button increaseBtn;
[SerializeField]
Button decreaseBtn;
public void Init(System.Action> dispatcher)
{
increaseBtn.onClick.AddListener(() => dispatcher(new IncreaseMsg()));
decreaseBtn.onClick.AddListener(() => dispatcher(new DecreaseMsg()));
}
public void Renderer(Model model)
{
display.text = model.count.ToString();
}
}
```
6. Create TEAManager class. Make it singleton if necessary. For the UniTEA object, provide an initialization function, an instance of Updater, and an instance of Renderer.
```c#
using UniTEA;
using UnityEngine;
public class TEAManager : MonoBehaviour
{
UniTEA tea;
[SerializeField]
new Renderer renderer;
void Start()
{
tea = new TEA(
() => (new Model(), Cmd.none),
new Updater(),
renderer
);
}
}
```
## Example
**TODO: Coming soon!**
**I'm happy if you contribute!**
## Development
1. Make your project and make directory Packages in it.
2. In Packages, `git clone https://github.com/uzimaru0000/UniTEA.git`
3. Install UniTEA from clone.
## Contribution
1. Fork it
2. Create your feature branch (git checkout -b my-new-feature)
3. Commit your changes (git commit -am 'Add some feature')
4. Push to the branch (git push origin my-new-feature)
5. Create new Pull Request