Legacy Documentation: Version 2018.1 (Go to current version)
Creating jobs
JobHandle and dependencies
Other Versions

Scheduling jobs

To schedule a job in the main thread, you must:

  • Instantiate the job.
  • Populate the job’s data.
  • Call the Schedule method.

Calling Schedule puts the job into the job queue for execution at the appropriate time. Once scheduled, you cannot interrupt a job.

Note: You can only call Schedule from the main thread.

An example of scheduling a job

// Create a native array of a single float to store the result. This example waits for the job to complete for illustration purposes
NativeArray<float> result = new NativeArray<float>(1, Allocator.TempJob);

// Set up the job data
MyJob jobData = new MyJob();
jobData.a = 10;
jobData.b = 10;
jobData.result = result;

// Schedule the job
JobHandle handle = jobData.Schedule();

// Wait for the job to complete

// All copies of the NativeArray point to the same memory, you can access the result in "your" copy of the NativeArray
float aPlusB = result[0];

// Free the memory allocated by the result array

Did you find this page useful? Please give it a rating:

Creating jobs
JobHandle and dependencies
Copyright © 2020 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。