Game.EndFrameBarrier
Assembly:
Namespace:
Type:
Base:
Summary:
EndFrameBarrier is a utility system used to coordinate end-of-frame command buffer execution and to collect/synchronize job handles produced by systems that record EntityCommandBuffer work. Typical responsibilities:
- Provide a command buffer that defers structural changes until the end of the simulation frame.
- Allow systems to obtain a parallel-writer variant of the ECB for use from jobs.
- Accept JobHandles from producer systems so the barrier can ensure those jobs complete before playback/structural changes are applied.
In the provided IndustrialAISystem example, an EndFrameBarrier instance is used to create a parallel EntityCommandBuffer (via CreateCommandBuffer().AsParallelWriter()) and to register the system's Dependency via AddJobHandleForProducer so command buffer playback is synchronized with jobs.
Fields
-
private System.Diagnostics.Stopwatch m_Stopwatch
Used for simple profiling/timing of the barrier's work (for example measuring how long barrier operations or playback take). Systems that need to instrument how long command buffer playback or barrier processing takes commonly initialize and update this Stopwatch in OnCreate/OnUpdate for diagnostic purposes. -
private Unity.Jobs.JobHandle <producerHandle>k__BackingField
Backing field for the publicly exposed producerHandle property. Holds the combined JobHandle for all producer jobs that have registered with the barrier so that the barrier can wait on or combine with that handle before executing/playback.
Properties
public Unity.Jobs.JobHandle producerHandle { get; private set }
Exposes the JobHandle that represents all jobs which have produced commands targeted at this barrier. The property is set by the barrier (or by AddJobHandleForProducer) when systems register their job dependencies, and it is used internally to ensure playback occurs only after producers have finished. Consumers should read this to chain further dependencies or let the barrier manage waiting.
Constructors
public EndFrameBarrier()
Constructs the barrier instance. Typical initialization tasks include preparing internal state used for command buffer collection/playback and initializing any profiling Stopwatch. Systems retrieving the barrier should treat it as a managed system to obtain via World.GetOrCreateSystemManaged().
Methods
protected virtual OnCreate() : System.Void
Initializes barrier state when the system is created. Common actions:- Create or reset the internal Stopwatch for timing (m_Stopwatch).
- Initialize the producerHandle to a default (JobHandle) value.
- Prepare any internal buffers/collections used to store command buffers submitted during the frame. Derived or consuming systems will typically rely on the barrier being available after OnCreate; examples often call base.OnCreate() and then set up the Stopwatch for instrumentation.
(Additional commonly-present methods, not listed above, typically found on an EndFrameBarrier-like system)
- CreateCommandBuffer() : EntityCommandBuffer
Returns an EntityCommandBuffer which records structural changes to be played back by the barrier at the appropriate synchronization point (end of frame). Consumers often call .AsParallelWriter() on the returned ECB to use it from jobs.
- AddJobHandleForProducer(JobHandle handle) : void
Registers a producer JobHandle with the barrier so the barrier can ensure playback only occurs after the producer jobs complete. In practice systems call this after scheduling their jobs and passing their system Dependency to the barrier.
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
m_Stopwatch = new Stopwatch();
}
Additional usage excerpt (from IndustrialAISystem):
// Obtain the barrier and use it to create a parallel command buffer writer for jobs
m_EndFrameBarrier = World.GetOrCreateSystemManaged<EndFrameBarrier>();
var writer = m_EndFrameBarrier.CreateCommandBuffer().AsParallelWriter();
// After scheduling jobs that produce ECB commands, register the job handle with the barrier:
m_EndFrameBarrier.AddJobHandleForProducer(jobHandle);