docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Property Dependency

    Dependency

    The ECS-related data dependencies of the system.

    Declaration
    protected JobHandle Dependency { get; set; }
    Property Value
    Type Description
    JobHandle
    Remarks

    Before OnUpdate(), the Dependency property represents the combined job handles of any job that writes to the same components that the current system reads -- or reads the same components that the current system writes to. When you use Entities.ForEach or Job.WithCode, the system uses the Dependency property to specify a job’s dependencies when scheduling it. The system also combines the new job's JobHandle with Dependency so that any subsequent job scheduled in the system depends on the earlier jobs (in sequence).

    The following example illustrates an OnUpdate() implementation that relies on implicit dependency management. The function schedules three jobs, each depending on the previous one:

    protected override void OnUpdate()
    {
        Entities
            .WithName("ForEach_Job_One")
            .ForEach((ref AComponent c) =>
            {
                /*...*/
            })
            .ScheduleParallel();
    
    Entities
        .WithName("ForEach_Job_Two")
        .ForEach((ref AnotherComponent c) =>
        {
            /*...*/
        })
        .ScheduleParallel();
    
    NativeArray<int> result = new NativeArray<int>(1, Allocator.TempJob);
    
    Job
        .WithName("Job_Three")
        .WithDisposeOnCompletion(result)
        .WithCode(() =>
        {
            /*...*/
            result[0] = 1;
        })
        .Schedule();
    

    }

    You can opt out of this default dependency management by explicitly passing a JobHandle to Entities.ForEach or Job.WithCode. When you pass in a JobHandle, these constructions also return a JobHandle representing the input dependencies combined with the new job. The JobHandle objects of any jobs scheduled with explicit dependencies are not combined with the system’s Dependency property. You must set the Dependency property manually to make sure that later systems receive the correct job dependencies.

    The following OnUpdate() function illustrates manual dependency management. The function uses two [Entity.ForEach] constructions that schedule jobs which do not depend upon each other, only the incoming dependencies of the system. Then a Job.WithCode construction schedules a job that depends on both of the prior jobs, who’s dependencies are combined using JobHandle.CombineDependencies. Finally, the JobHandle of the last job is assigned to the Dependency property so that the ECS safety manager can propagate the dependencies to subsequent systems.

    protected override void OnUpdate()
    {
        JobHandle One = Entities
            .WithName("ForEach_Job_One")
            .ForEach((ref AComponent c) =>
            {
                /*...*/
            })
            .ScheduleParallel(this.Dependency);
    
    JobHandle Two = Entities
        .WithName("ForEach_Job_Two")
        .ForEach((ref AnotherComponent c) =>
        {
            /*...*/
        })
        .ScheduleParallel(this.Dependency);
    
    JobHandle intermediateDependencies =
        JobHandle.CombineDependencies(One, Two);
    
    NativeArray<int> result = new NativeArray<int>(1, Allocator.TempJob);
    
    JobHandle finalDependency = Job
        .WithName("Job_Three")
        .WithDisposeOnCompletion(result)
        .WithCode(() =>
        {
            /*...*/
            result[0] = 1;
        })
        .Schedule(intermediateDependencies);
    
    this.Dependency = finalDependency;
    

    }

    You can combine implicit and explicit dependency management (by using JobHandle.CombineDependencies); however, doing so can be error prone. When you set the Dependency property, the assigned JobHandle replaces any existing dependency, it is not combined with them.

    Note that the default, implicit dependency management does not include IJobChunk jobs. You must manage the dependencies for IJobChunk explicitly.

    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)