- To provide a simple and easy-to-use state management library for Unity.(Like Redux or Svelte.store)
- State Set/Read/Observe
- StateCollection
- DependencyState/HalfDependencyState
DependencyState / HalfDependencyState / ToStateObserver share a single subscription to the source among all observers (ShareLatest).
The result is computed once per change no matter how many observers there are, and a late observer receives the latest value immediately.
The subscription is connected on the first observer and disconnected when the last one is disposed (the cached value is discarded then).
Read() is not shared and always computes the current value.
| class | summary | implements interface |
|---|---|---|
| ObservableState SubjectState |
A state variable that can be observed | IStateReader, IStateObserver, IStateSetter, IDisposable |
| State | A state variable that cannot be observed but is more memory-efficient than ObservableState | IStateReader, IStateSetter |
| DependencyState | A state variable that derives results from other variables and triggers them | IStateReader, IStateObserver |
| HalfDependencyState | A state variable that derive results from other variables and their own variable and trigger them | IStateReader, IStateSetter, IStateObserver |
| ObservableListState | A collection of StateVariables (internally implemented as a List) | IStateReader, IStateSetter, IStateCollectionObserver, IStateCollectionElementObserver, IStateCollectionElementSetter, IStateCollectionModifier |
| ObservableDictionaryState | A collection of StateVariables (internally implemented as a Dictionary) | IStateReader, IStateSetter, IStateCollectionObserver, IStateCollectionElementObserver, IStateCollectionElementSetter, IStateCollectionModifier |
Each State is bound to the appropriate context of the DI container, and each object obtains the necessary dependencies.
// ----- Installer
var testState = new ObservableState<Test>(new Test());
testState.AddTo(this);
Container.BindInstance<IStateReader<Test>>(testState);
Container.BindInstance<IStateObserver<Test>>(testState);
Container.BindInstance<IStateSetter<Test>>(testState);[Inject] IStateObserver<Test> _testObserver;
[Inject] IStateReader<Test> _testReader;
[Inject] IStateSetter<Test> _testSetter;
void Start()
{
// How to use variable
_testObserver.Observe().Subscribe(test => {}).AddTo(this);
Test test = _testReader.Read();
_testSetter.Set(new Test());
// Observer can also read
test = _testObserver.Read();
// Setter can also read
test = _testSetter.Read();
}- Open the Package Manager
- Press [+▼] button and click Add package from git URL...
- Enter the following:
https://github.com/tanitaka-tech/StateVariable.git
- Install NugetForUnity.
- Search for
StateVariablein the NugetForUnity window. - Click the
Installbutton.
- Run the following:
dotnet add package StateVariable --version 1.4.0