I’m trying to do thousands of BoxcastCommands in a jobified way in a custom inspector, but I still seem to be running everything on the main thread. The code I have so far looks like this:
NativeArray<RaycastHit> results =
new NativeArray<RaycastHit>(nodesProperty.arraySize, Allocator.TempJob);
NativeArray<BoxcastCommand> commands =
new NativeArray<BoxcastCommand>(nodesProperty.arraySize, Allocator.TempJob);
for (int i = 0; i < nodesProperty.arraySize; ++i)
{
Vector3 nodePosition = editorNodes[i].position;
commands[i] = new BoxcastCommand(nodePosition + Vector3.up, Vector3.one * diameterProperty.floatValue * 0.5f, Quaternion.identity, Vector3.down, layerMask: LayerMask.GetMask("Ground", "Environment"));
}
JobHandle handle = BoxcastCommand.ScheduleBatch(commands, results, 1, default(JobHandle));
handle.Complete();
Obviously this can be chunked, but how do I do this across threads in the editor?
Try putting a JobHandle.ScheduleBatchedJobs() between the last two lines. I don’t know how well it works in edit mode, but it’s worth a try. Also you should probably bump that 1 up to like 200 or so.
Edit* - I’ve even converted my creation of BoxcastCommands to an IJobParallelFor struct, but am still using only about 5% of my CPU resources (16 core, 32 thread cpu).
If possible, feed the handle into another job, or at least delay calling Complete until you need to.
Increasing the number can decrease the number of threads running if you don’t have enough items, on the other hand, lower numbers are not always faster overall due to scheduling costs.