Game.EndFrameBarrier
Assembly: Game
Namespace: Game.Simulation
Type: class
Base: Unity.Entities.SystemBase (end-of-frame barrier system)
Summary:
EndFrameBarrier is a managed end-of-frame barrier/system that provides an EntityCommandBuffer targeted to be played back at the end of the frame. It is intended to be used by game systems which schedule jobs that record entity commands (add/remove components, create/destroy entities, etc.). The barrier exposes a command buffer (including a ParallelWriter) and accepts producer JobHandles so it can ensure playback happens only after all producer jobs complete. In the provided VehicleSpawnSystem usage, EndFrameBarrier is used to create a parallel command buffer for jobs that delete vehicles and remove TripSource components, and to register the scheduled job's handle so the barrier will wait for completion before applying the buffered commands.
Fields
-
private System.Diagnostics.Stopwatch m_Stopwatch
Used internally for timing/profiling of barrier operations (e.g., measuring time spent playing back the command buffer). In practice this can help detect expensive end-of-frame command playback. -
private Unity.Jobs.JobHandle <producerHandle>k__BackingField
Backing field holding the JobHandle that represents the combined producers scheduled against this barrier. The barrier uses this to know when it is safe to play back its command buffer (i.e., after all producer jobs have finished).
Properties
public Unity.Jobs.JobHandle producerHandle { get; private set }
Exposes the combined JobHandle of producer jobs to other systems (read-only). Systems scheduling producer jobs should give their JobHandle to the barrier (via AddJobHandleForProducer) so the barrier can chain dependencies and ensure the command buffer playback waits on those producers.
Constructors
public EndFrameBarrier()
Constructs the barrier system. Typical initialization will set up internal state (including stopwatch and an initial empty producer handle). When obtained via World.GetOrCreateSystemManaged(), the system is registered in the world and can be used by other systems (e.g., VehicleSpawnSystem).
Methods
protected virtual OnCreate() : System.Void
Initializes the barrier (called when the system is created). Common work performed here: allocate or initialize internal profiling tools (like m_Stopwatch), set up any internal command buffer management structures, and prepare the system to accept producer job handles.
Additional common/expected methods (used by VehicleSpawnSystem):
-
public EntityCommandBuffer CreateCommandBuffer()
/public EntityCommandBuffer.ParallelWriter CreateCommandBuffer().AsParallelWriter()
Returns an EntityCommandBuffer (or a ParallelWriter) that jobs may use to record structural changes. Jobs should use the ParallelWriter variant when they run in parallel. -
public void AddJobHandleForProducer(Unity.Jobs.JobHandle producer)
Register a producer JobHandle with the barrier so it can combine/chain dependencies and ensure playback does not occur until producers are finished. VehicleSpawnSystem calls this after scheduling its TrySpawnVehiclesJob: m_EndFrameBarrier.AddJobHandleForProducer(jobHandle).
Notes on expected lifecycle/interaction:
- Systems will typically obtain the barrier (World.GetOrCreateSystemManaged
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// Cache the barrier system so we can create command buffers for jobs
m_EndFrameBarrier = World.GetOrCreateSystemManaged<EndFrameBarrier>();
}
[Preserve]
protected override void OnUpdate()
{
// Create a parallel writer from the barrier for use inside jobs
var commandBufferParallel = m_EndFrameBarrier.CreateCommandBuffer().AsParallelWriter();
// Schedule your job(s) that record entity commands, passing the parallel writer
JobHandle jobHandle = new MyRecordingJob
{
m_CommandBuffer = commandBufferParallel,
// ... other data ...
}.Schedule(dependency: Dependency);
// Tell the barrier about the producer job so playback will wait for it
m_EndFrameBarrier.AddJobHandleForProducer(jobHandle);
// store dependency if needed
Dependency = jobHandle;
}
This pattern is exactly how VehicleSpawnSystem uses EndFrameBarrier: it creates the barrier in OnCreate, uses CreateCommandBuffer().AsParallelWriter() when scheduling TrySpawnVehiclesJob, and then calls m_EndFrameBarrier.AddJobHandleForProducer(jobHandle) so the barrier will safely apply the entity changes after producers finish.