Skip to content

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

License

Notifications You must be signed in to change notification settings

domenkoneski/unity-jobs-callback

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages