Skip to content

Latest commit

 

History

History
42 lines (33 loc) · 1.42 KB

README.md

File metadata and controls

42 lines (33 loc) · 1.42 KB

unity-jobs-callback

Job Helper utillity that enables you to create callbacks for your completed jobs and safely disposes any Native Containers.

How to use

  1. Import JobHelper.cs to your project.

  2. 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.

  1. 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]);
});
  1. Done! If you want an extended use case check JobHelperExamples.cs for more control.