Use classes and other managed types with Unity's C# Job System.
The Job System only accepts blittable struct types for jobs.
This package makes it easy to use managed types as jobs by providing blittable structs that reference managed objects using GCHandle
and forward job execution to them.
IJob
types using ManagedJob
IJobFor
types using ManagedJobFor
IJobParallelFor
types using ManagedJobParallelFor
IJobParallelForTransform
types using ManagedJobParallelForTransform
GCHandle
if you call Schedule
/ Run
methods and their variations directly on the wrapper structsEither:
openupm add com.gilzoide.managed-jobs
https://github.com/gilzoide/unity-managed-jobs.git#1.0.0
Assets
or Packages
folder.using Unity.Jobs;
using Gilzoide.ManagedJobs;
// 1. Create your managed job type
public class MyManagedJobClass : IJob
{
public string Message = "Fields with managed types are supported!";
public void Execute()
{
Debug.Log($"Job is being executed! Here's the message: '{Message}'");
}
}
// 2. Schedule the job by using the wrapper ManagedJob struct type
var myManagedJobObject = new MyManagedJobClass();
var jobHandle = new ManagedJob(myManagedJobObject).Schedule();
// 3. Complete the jobHandle or use it as dependency to other jobs as usual
jobHandle.Complete();
// 4. Enjoy 🍾