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 schedule [IJobEntity] execution, 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 multiple IJobEntity jobs, each depending on the previous one:
[BurstCompile]
partial struct JobOne : IJobEntity
{
public void Execute(in AComponent c)
{
/*...*/
}
}
[BurstCompile]
partial struct JobTwo : IJobEntity
{
public void Execute(in AnotherComponent c)
{
/.../
}
}
[BurstCompile]
struct JobThree : IJob
{
[DeallocateOnJobCompletion] public NativeArray<int> Result; // Automatically disposed when job completes
public void Execute()
{
/*...*/
Result[0] = 1;
}
}
protected override void OnUpdate()
{
// Implicit dependency chaining: each ScheduleParallel updates SystemBase.Dependency
new JobOne().ScheduleParallel();
new JobTwo().ScheduleParallel();
// Final job depends on previous via implicit Dependency and disposes its NativeArray automatically
var jobThree = new JobThree
{
Result = new NativeArray<int>(1, Allocator.TempJob)
};
jobThree.Schedule();
}
You can opt out of this default dependency management by explicitly passing a JobHandle when scheduling an IJobEntity. When you pass in a handle, the scheduling API returns a new handle representing the input dependencies combined with the new job. Handles from jobs scheduled with explicit dependencies are not automatically combined with the system’s Dependency property. You must set the Dependency property manually to propagate dependencies.
The following OnUpdate() function illustrates manual dependency management. The function schedules two IJobEntity jobs that do not depend upon each other (only the incoming system Dependency). Then a third job (IJobEntity or an Unity.Jobs.IJob) depends on both of the prior jobs; their handles are combined using CombineDependencies(JobHandle,JobHandle). Finally, the resulting handle is assigned to the Dependency property so that the ECS safety manager can propagate the dependencies to subsequent systems.
[BurstCompile]
partial struct JobOne : IJobEntity
{
public void Execute(in AComponent c)
{
/*...*/
}
}
[BurstCompile]
partial struct JobTwo : IJobEntity
{
public void Execute(in AnotherComponent c)
{
/.../
}
}
[BurstCompile]
struct JobThree : IJob
{
[DeallocateOnJobCompletion] public NativeArray<int> Result; // Automatically disposed when job completes
public void Execute()
{
/*...*/
Result[0] = 1;
}
}
protected override void OnUpdate()
{
// Explicitly opt-out of implicit chaining by scheduling with incoming system Dependency
JobHandle One = new JobOne().ScheduleParallel(this.Dependency);
JobHandle Two = new JobTwo().ScheduleParallel(this.Dependency);
JobHandle intermediateDependencies =
JobHandle.CombineDependencies(One, Two);
var jobThree = new JobThree
{
Result = new NativeArray<int>(1, Allocator.TempJob)
};
JobHandle finalDependency = jobThree.Schedule(intermediateDependencies);
// Propagate combined dependency to subsequent systems
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.