Skip to content

Repository files navigation

openupmlicense

Docs (English, 日本語)

Mission of this Library

  • Decouple the classes that manage the flow of processes from the classes that trigger events.
  • Provide a means to clearly and concisely express the flow of processes.

Recommended Use Cases

  • Handling events that lead to processes that are risky to run in parallel, such as scene transitions or API calls.
  • Changing the events to wait for according to the flow of processes.

Usage

  1. Use RequestPusher to send requests when events are triggered in each class.
  2. Use RequestConsumer to await the push of requests.
  3. Use ConcurrentProcess to wait for multiple RequestConsumer instances in parallel and describe the processing for each request individually when it arrives.

Usage Example

① Bind RequestHandler (Zenject Example)

// ----- In some installer// You need to define a unique type for each requestvareffectRequestHandler=newRequestHandler<EffectRequest>();Container.BindInstance<IRequestPusher<EffectRequest>>(effectRequestHandler);varcloseRequestHandler=newRequestHandler<CloseRequest>();Container.BindInstance<IRequestPusher<CloseRequest>>(closeRequestHandler);varnextSceneRequestHandler=newRequestHandler<NextSceneRequest>();Container.BindInstance<IRequestPusher<NextSceneRequest>>(nextSceneRequestHandler);varwaitRequestClass=newWaitRequestClass(effectRequestConsumer:effectRequestHandler,closeRequestConsumer:closeRequestHandler,nextSceneRequestConsumer:nextSceneRequestHandler);

② Push Request (R3 Example)

// ----- In some object[SerializeField]privateButton_closeButton;[Inject]privateIRequestPusher<CloseRequest>_closeRequestPusher;privatevoidStart(){_closeButton.OnClickAsObservable().Subscribe(_ =>_closeRequestPusher.PushRequest(newCloseRequest())).AddTo(this);}

③ Wait Request

awaitConcurrentProcess.Create(// When the waitTask is exited, the onPassedTask await is executed. At that time, the waitTask of other Processes is canceled.// This specification ensures that only one Request is handled at a time.Process.Create(waitTask:async ct =>awaitEffectRequestConsumer.WaitRequestAndConsumeAsync(ct),onPassedTask:async ct =>{awaitPlayEffectAsync(ct);// Returning Continue in onPassedTask continues the parallel waiting for RequestsreturnProcessContinueType.Continue;}),Process.Create(waitTask:async ct =>awaitCloseRequestConsumer.WaitRequestAndConsumeAsync(ct),onPassedTask:async ct =>{awaitCloseAsync(ct);// Returning Break in onPassedTask exits the await of LoopProcessAsyncreturnProcessContinueType.Break;}),Process.Create(waitTask:async ct =>awaitNextSceneRequestConsumer.WaitRequestAndConsumeAsync(ct),onPassedTask:async ct =>{awaitLoadNextSceneAsync(ct);returnProcessContinueType.Break;}),).LoopProcessAsync(cancellationToken:ct);

LiteRequestBroker

LiteRequestBroker is a class that can be used to send requests without using the RequestHandler class.

It is good to use in the following cases:

  • The request does not have any parameters
  • If the request is not being waited simultaneously

① Bind LiteRequestBroker

// ----- In some installervarliteRequestBroker=newLiteRequestBroker();Container.BindInstance<ILiteRequestPusher>(liteRequestBroker);Container.BindInstance<ILiteRequestConsumer>(liteRequestBroker);

② Push Request (R3 Example)

// ----- In some object[SerializeField]privateButton_closeButton;[Inject]privateILiteRequestPusher_liteRequestPusher;privatevoidStart(){_closeButton.OnClickAsObservable()// I reccommend defining a const string for each request.Subscribe(_ =>_liteRequestPusher.PushRequest("CloseRequest")).AddTo(this);}

③ Wait Request

awaitConcurrentProcess.Create(Process.Create(waitTask:async ct =>awaitLiteRequestConcumer.WaitRequestAndConsumeAsync("CloseRequest",ct),onPassedTask:async ct =>{awaitCloseAsync(ct);returnProcessContinueType.Break;}),// ...).LoopProcessAsync(cancellationToken:ct);

Logging

You can enable logging by defining the following:

UNITY_PROCESS_MANAGER_LOGGER

ConcurrentProcess Logging

awaitConcurrentProcess.CreateWithLog(Logger,// Some Microsoft.Extensions.Logging implements class.(I recommend using ZLogger)MoveToLicensesProcessProvider// Some IProcessProvider implements class.(This is the class you created)).LoopProcessAsync(cancellationToken:cancellationToken);

LiteRequestBroker Logging

varliteRequestBroker=newLiteRequestBroker(Logger);

Installation ☘️

Install via git URL

  1. Open the Package Manager
  2. Press [+▼] button and click Add package from git URL...
  3. Enter the following:

Install via OpenUPM

openupm add com.tanitaka-tech.unity-process-manager

Contribution 🎆

Issues and PRs are very welcome! I'll take a look when I have free time. Also, if you're interested in this project, please give it a star!

Requirement

About

A package that supports expressing process-based operations in Unity

Topics

Resources

Stars

12 stars

Watchers

1 watching

Forks

Releases

Contributors

Languages