Using jobs with archetype chunk query / CreateArchetypeChunkArray ?

This is a sample:

public class SomeSystem : JobComponentSystem {
    private ComponentGroup group;

    protected override void OnCreateManager() {
        this.group = GetComponentGroup(typeof(Position));
    }

    protected override JobHandle OnUpdate(JobHandle inputDeps) {
        NativeArray<ArchetypeChunk> chunks = this.group.CreateArchetypeChunkArray(Allocator.TempJob);

        // Prepare the job
        Job job = new Job() {
            positionType = GetArchetypeChunkComponentType<Position>(),
            chunks = chunks
        };

        return job.Schedule(chunks.Length, 64, inputDeps);
    }

    private struct Job : IJobParallelFor {
        // You need a type so you can get a NativeArray of this component
        public ArchetypeChunkComponentType<Position> positionType;

        [ReadOnly]
        [DeallocateOnJobCompletion]
        public NativeArray<ArchetypeChunk> chunks;

        public void Execute(int index) {
            Process(this.chunks[index]);
        }

        private void Process(ArchetypeChunk chunk) {
            NativeArray<Position> positions = chunk.GetNativeArray(this.positionType);
            // You can loop through positions here and do what you want to do
        }
    }
}
4 Likes