Access dynamic buffers from jobs
If a job needs to lookup one or more buffers in its code, the job needs to use a BufferLookup lookup table. You create these in systems and then pass them to jobs that need them.
Modify the job
In the job that requires random access to a dynamic buffer:
- Add a ReadOnlyBufferLookupmember variable.
- In the IJobEntity.Executemethod, index theBufferLookuplookup table by an entity. This provides access to the dynamic buffer attached to the entity.
public partial struct AccessDynamicBufferJob : IJobEntity
{
    [ReadOnly] public BufferLookup<ExampleBufferComponent> BufferLookup;
    public void Execute()
    {
        // ...
    }
}
Modify the systems
In systems that create instances of the job:
- Add a BufferLookupmember variable.
- In OnCreate, useSystemState.GetBufferLookupto assign theBufferLookupvariable.
- At the beginning of OnUpdate, callUpdateon theBufferLookupvariable. This updates the lookup table.
- When you create an instance of the job, pass the lookup table to the job.
public partial struct AccessDynamicBufferFromJobSystem : ISystem
{
    private BufferLookup<ExampleBufferComponent> _bufferLookup;
    public void OnCreate(ref SystemState state)
    {
        _bufferLookup = state.GetBufferLookup<ExampleBufferComponent>(true);
    }
    public void OnUpdate(ref SystemState state)
    {
        _bufferLookup.Update(ref state);
        var exampleBufferAccessJob = new AccessDynamicBufferJob { BufferLookup = _bufferLookup };
        exampleBufferAccessJob.ScheduleParallel();
    }
}