Unity Job System and Burst Compiler: Getting Started

In this tutorial, you’ll learn how to use Unity’s Job System and Burst compiler to create efficient code to simulate water filled with swimming fish. By Ajay Venkat.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 4 of 4 of this article. Click here to view the first page.

Scheduling the Movement Job

To run PositionUpdateJob, you have to schedule it. Like before, you'll schedule the job on Update() and complete it on LateUpdate().

First, add the following variables to the top of the class:

private PositionUpdateJob positionUpdateJob;

private JobHandle positionUpdateJobHandle;

This is a reference to the job and its handle, so you can access it throughout Update() and LateUpdate().

Place this code in the Update():

// 1
positionUpdateJob = new PositionUpdateJob()
{
    objectVelocities = velocities,
    jobDeltaTime = Time.deltaTime,
    swimSpeed = this.swimSpeed,
    turnSpeed = this.turnSpeed,
    time = Time.time,
    swimChangeFrequency = this.swimChangeFrequency,
    center = waterObject.position,
    bounds = spawnBounds,
    seed = System.DateTimeOffset.Now.Millisecond
};

// 2
positionUpdateJobHandle = positionUpdateJob.Schedule(transformAccessArray);

First, all the variables within the main thread set the job's data. seed gets the current millisecond from the system time to ensure a different seed for each call.

Secondly, you schedule positionUpdateJob. Note that each job type has its own Schedule() parameters. A IJobParallelForTransform takes a TransformAccessArray.

Finally, add this into LateUpdate():

 positionUpdateJobHandle.Complete(); 

This ensures the completion of the job before moving onto the next Update cycle.

The structure of FishGenerator.cs should look like this:

Fish Generator Code Structure

Now, save the file and enter Unity. Press Play and watch those fish go!

Fish Swimming in Water

While 200 swimming fish is impressive, the system can do a whole lot better. For fun, run a little stress test by increasing the amount of fish to 5,000:

5,000 Fish Stress Test

5,000 fish swimming in simulated water, and it's still running at around 200 FPS. Clearly, the Job System is very impressive. However, the Burst compiler plays a major role in optimizing your code.

Inspecting the Profiler

A Job System manages a group of worker threads across multiple cores. The Profiler shows the segmentation of work.

Profiler Inspection

Notice how there are multiple worker threads running the scripts in parallel, reducing the duration of the process.

Where to Go From Here?

Download the complete project using the Download Materials button at the top or bottom of this tutorial.

In this tutorial, you learned how to:

  • Use the Job System to multithread loops.
  • Implement the Burst compiler.
  • Modify the properties of transforms over multiple threads.

This project is only the beginning; there's so much more you can add. I'm interested to see what you come up with! Implementing ECS with the fishes would be a great next step in optimizing this game.

Did you enjoy this tutorial? Want to learn more? Check out our book Unity Games by Tutorials, which has more info on making games with Unity.

If you want to learn more about Unity's Job System, check out What Is a Job System? by Unity.

You'll also find useful information in the official Job System Manual.

If you have any suggestions, questions or you want to show off what you did to improve this project, join the discussion below.