docs.unity3d.com
    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 a command buffer from a Job, you must add the of that Job to this buffer system's dependency list by calling this function. Otherwise, the buffer system could execute the commands currently in the command buffer while the writing Job is still in progress.

    Examples

    The following example illustrates how to use one of the default EntityCommandBuffer systems. The code selects all entities that have one custom component, in this case, AsyncProcessInfo, and processes each entity in the Execute() function of an IJobForEachWithEntity<T0> Job (the actual process is not shown since that part of the example is hypothetical). After processing, the Job uses an EntityCommandBuffer to remove the ProcessInfo component and add an 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{}
    
    public class AsyncProcessJobSystem : JobComponentSystem
    {
        [BurstCompile]
        public struct ProcessInBackgroundJob : IJobForEachWithEntity<ProcessInfo>
        {
            [ReadOnly]
            public EntityCommandBuffer.ParallelWriter ConcurrentCommands;
    
            public void Execute(Entity entity, int index, [ReadOnly] ref ProcessInfo info)
            {
                // Process based on the ProcessInfo component,
                // then remove ProcessInfo and add a ProcessCompleteTag...
    
                ConcurrentCommands.RemoveComponent<ProcessInfo>(index, entity);
                ConcurrentCommands.AddComponent(index, entity, new ProcessCompleteTag());
            }
        }
    
        protected override JobHandle OnUpdate(JobHandle inputDeps)
        {
            var job = new ProcessInBackgroundJob();
    
            var ecbSystem =
                World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
            job.ConcurrentCommands = ecbSystem.CreateCommandBuffer().AsParallelWriter();
    
            var handle = job.Schedule(this, inputDeps);
            ecbSystem.AddJobHandleForProducer(handle);
    
            return handle;
        }
    }
    Back to top
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023