Configurable MonoBehaviour
and ScriptableObject
subclasses wrapping
TaskFactory
objects plus a collection of TaskScheduler
implementations with limited concurrency.
Either:
- Use the openupm registry and install this package using the openupm-cli:
openupm add com.gilzoide.taskfactory-object
- Install via Unity Package Manager using this repository URL and tag:
https://github.com/gilzoide/TaskFactoryObject.git#1.0.0
- Clone this repository directly inside your project's
Assets
orPackages
folder.
The TaskSchedulers module comes with the following implementations:
- SyncTaskScheduler: run tasks
in the current
SynchronizationContext
, which by default is Unity's Main Thread. Default maximum concurrency isint.MaxValue
, that is, process every queued task in a single frame. - ManagedThreadPoolTaskScheduler:
run tasks in the Managed Thread Pool.
Default maximum concurrency is
Environment.ProcessorCount
. - OwnThreadsTaskScheduler:
creates its own threads and run tasks on them. Uses a
SemaphoreSlim
for sleeping threads until there is work to be done. Threads can have their
name and background flag configured. Default maximum concurrency is
Environment.ProcessorCount
.
TaskFactoryComponent is a MonoBehaviour
subclass with configurations for creating TaskFactory
and TaskScheduler
objects.
TaskFactoryScriptableObject is its
ScriptableObject
counterpart, both work in the same way.
Their Scheduler
property gets a configured TaskScheduler
and their
Factory
property gets a configured TaskFactory
. These are created on demand
when accessing the properties.
The TaskScheduler
and TaskFactory
's default CancellationToken
are tied to
the lifetime of the objects and will be canceled automatically on
TaskFactoryComponent
's OnDestroy
and TaskFactoryScriptableObject
's
OnDisable
methods. When that happens, no more tasks can be scheduled and
pending ones will be dropped silently. One can also manually cancel them by
calling DestroyFactory
. Accessing the Scheduler
or Factory
properties
afterwards recreates them.
If MaximumConcurrency
is 0 or a negative number, the created TaskScheduler
will use its own default maximum concurrency, which depends on the
implementation.
Usage example:
using System.Threading.Tasks;
using UnityEngine;
using Gilzoide.TaskFactoryObject;
public class SomeOtherScript : MonoBehaviour
{
public TaskFactoryComponent FactoryComponent;
public TaskFactoryScriptableObject FactorySO;
async void Start()
{
// using TaskScheduler directly
Task task = new Task(() =>
{
Debug.Log("This runs in the TaskScheduler configured by FactoryComponent!");
});
task.Start(FactoryComponent.Scheduler);
await task;
// using the configured TaskFactory
await FactoryComponent.Factory.StartNew(() =>
{
Debug.Log("This one too (FactoryComponent)!");
});
// using TaskScheduler directly, this time from FactorySO
await Task.Delay(100).ContinueWith(_delayTask =>
{
Debug.Log("This continuation runs in the TaskScheduler configured by FactorySO!");
}, FactorySO.Scheduler);
Debug.Log("All done!");
}
}
The TaskFactoryConfig is a serializable class
with configurations for creating TaskScheduler
and TaskFactory
objects.
This is used by both TaskFactoryComponent
and TaskFactoryScriptableObject
.
Methods:
TaskScheduler CreateScheduler(CancellationToken cancellationToken = default)
: creates a newTaskScheduler
with the defined configuration.TaskFactory CreateFactory(CancellationToken cancellationToken = default)
: creates a newTaskFactory
with the defined configuration. ATaskScheduler
is also created viaCreateScheduler
and passed to it.TaskFactory CreateFactory(TaskScheduler taskScheduler, CancellationToken cancellationToken = default)
: creates a newTaskFactory
with the defined configuration, using a specific scheduler object.