Skip to content

Latest commit

 

History

History
89 lines (68 loc) · 5.11 KB

README.md

File metadata and controls

89 lines (68 loc) · 5.11 KB

license

nuget

Mission of this Library

  • To provide a simple and easy-to-use state management library for Unity.(Like Redux or Svelte.store)

Features 🌟

  • State Set/Read/Observe
  • StateCollection
  • DependencyState/HalfDependencyState

Shared observation of DependencyState

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.

State List

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

Usage Example(This Example used a Zenject)

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();
}

Installation ☘️

Unity

Install via git URL

  1. Open the Package Manager
  2. Press [+▼] button and click Add package from git URL...
  3. Enter the following: https://github.com/tanitaka-tech/StateVariable.git

Install via NugetForUnity

  1. Install NugetForUnity.
  2. Search for StateVariable in the NugetForUnity window.
  3. Click the Install button.

Install via nuget

  1. Run the following:
dotnet add package StateVariable --version 1.4.0

Required