docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Method AddJobHandleForProducer

    AddJobHandleForProducer(JobHandle)

    Adds the specified JobHandle to this system's list of dependencies.

    Declaration
    public void AddJobHandleForProducer(JobHandle producerJob)
    Parameters
    Type Name Description
    JobHandle producerJob

    The JobHandle of a Job which this buffer system should wait for before playing back its pending command buffers.

    Remarks

    When you write to an EntityCommandBuffer from a Job, you must add the JobHandle of that Job to this EntityCommandBufferSystem's input dependencies by calling this function. Otherwise, this system could attempt to execute the command buffer contents while the writing Job is still running, causing a race condition.

    Examples

    The following example illustrates how to use one of the default EntityCommandBufferSystems. The code selects all entities that have one custom component, in this case, AsyncProcessInfo, and processes each entity in the Execute() function of an IJobEntity Job. After processing, the Job uses a EntityCommandBuffer to remove the ProcessInfo component and add a ProcessCompleteTag component. Another system could use the ProcessCompleteTag to find entities that represent the end results of the process.

    public struct ProcessInfo : IComponentData { public float Value; }
    public struct ProcessCompleteTag : IComponentData { }
    
    [BurstCompile]
    public partial struct ProcessInBackgroundJob : IJobEntity
    {
        public EntityCommandBuffer.ParallelWriter ConcurrentCommands;
    
        public void Execute(Entity entity, [ChunkIndexInQuery] int chunkIndex, in ProcessInfo info)
        {
            // ...hypothetical processing goes here...
    
            // Remove ProcessInfo and add a ProcessCompleteTag...
            ConcurrentCommands.RemoveComponent<ProcessInfo>(chunkIndex, entity);
            ConcurrentCommands.AddComponent<ProcessCompleteTag>(chunkIndex, entity);
        }
    }
    
    public partial class AsyncProcessJobSystem : SystemBase
    {
        protected override void OnUpdate()
        {
            // Get a reference to the system that will play back
            var ecbSystem = World.GetOrCreateSystemManaged<NonSingletonECBSystem>();
            var ecb = ecbSystem.CreateCommandBuffer();
    
            // Pass the command buffer to the writer job, using its ParallelWriter interface
            var job = new ProcessInBackgroundJob
            {
                ConcurrentCommands = ecb.AsParallelWriter(),
            };
            Dependency = job.ScheduleParallel(Dependency);
    
            // Register the writer job with the playback system as an input dependency
            ecbSystem.AddJobHandleForProducer(Dependency);
        }
    }
    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)