Job Helper utillity that enables you to create callbacks for your completed jobs and safely disposes any Native Containers.
-
Import JobHelper.cs to your project.
-
Create your Job struct and implement IJobDisposable interface and make sure to dispose any NativeContainer in the OnDispose method. Example of a simple job:
public struct SimpleJob : IJob, IJobDisposable {
public NativeArray<float> result;
public void Execute() {
for (int i = 0; i < 1000000; i++) {
var s = Mathf.Sin(10 * i);
}
result[0] = 5;
}
public void OnDispose() {
result.Dispose();
}
}
OnDispose()
gets called OnDestroy()
, e.g. closing the game. Make sure to dispose Persistent type allocations here. You can also call executor.Dispose()
to manually dispose anything disposable.
- Create an instance of your Job and schedule it with JobHelper.
NativeArray<float> result = new NativeArray<float>(1, Allocator.Persistent);
SimpleJob job = new SimpleJob() {
result = result
};
JobHelper.AddScheduledJob(job, job.Schedule(), (jobExecutor) => {
Debug.LogFormat("Job has completed in {0}s and {1} frames!", jobExecutor.Duration, jobExecutor.FramesTaken);
// Result is available. LateUpdate() context.
Debug.Log(result[0]);
});
- Done! If you want an extended use case check JobHelperExamples.cs for more control.