docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    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:

    1. Add a ReadOnly BufferLookup member variable.
    2. In the IJobEntity.Execute method, index the BufferLookup lookup 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:

    1. Add a BufferLookup member variable.
    2. In OnCreate, use SystemState.GetBufferLookup to assign the BufferLookup variable.
    3. At the beginning of OnUpdate, call Update on the BufferLookup variable. This updates the lookup table.
    4. 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();
        }
    }
    
    In This Article
    Back to top
    Copyright © 2024 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)