- 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.
- 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.
- Use
RequestPusherto send requests when events are triggered in each class. - Use
RequestConsumerto await the push of requests. - Use
ConcurrentProcessto wait for multipleRequestConsumerinstances in parallel and describe the processing for each request individually when it arrives.
// ----- 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);// ----- In some object[SerializeField]privateButton_closeButton;[Inject]privateIRequestPusher<CloseRequest>_closeRequestPusher;privatevoidStart(){_closeButton.OnClickAsObservable().Subscribe(_ =>_closeRequestPusher.PushRequest(newCloseRequest())).AddTo(this);}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 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
// ----- In some installervarliteRequestBroker=newLiteRequestBroker();Container.BindInstance<ILiteRequestPusher>(liteRequestBroker);Container.BindInstance<ILiteRequestConsumer>(liteRequestBroker);// ----- 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);}awaitConcurrentProcess.Create(Process.Create(waitTask:async ct =>awaitLiteRequestConcumer.WaitRequestAndConsumeAsync("CloseRequest",ct),onPassedTask:async ct =>{awaitCloseAsync(ct);returnProcessContinueType.Break;}),// ...).LoopProcessAsync(cancellationToken:ct);You can enable logging by defining the following:
UNITY_PROCESS_MANAGER_LOGGER
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);varliteRequestBroker=newLiteRequestBroker(Logger);- Open the Package Manager
- Press [+▼] button and click Add package from git URL...
- Enter the following:
openupm add com.tanitaka-tech.unity-process-managerIssues 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!