要在主线程中安排作业,您必须:
- 实例化作业。
- 填充作业的数据。
- 调用Schedule方法。
调用Schedule
将作业放入作业队列以便在适当的时间执行。一旦安排,你就不能打断工作。
注意:您只能Schedule
从主线程调用。
安排工作的一个例子
// 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
handle.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
result.Dispose();