Skip to content

Game.EndFrameBarrier

Assembly: Game
Namespace: Game

Type: class

Base: Unity.Entities.SystemBase (managed system that provides an end-of-frame command buffer)

Summary: EndFrameBarrier is a managed ECS system that provides a safe, end-of-frame EntityCommandBuffer for scheduling structural changes from systems and jobs. It integrates with Unity.Jobs.JobHandle flow so producers can return JobHandles that the barrier will track and complete as needed. The barrier is typically used by systems that need to enqueue commands (create/destroy/set components, add/remove components) that must be played back after simulation or at the end of the frame to avoid structural changes while jobs are running. It also contains lightweight timing/profiling support (a Stopwatch) used for debugging/performance measurements in-game.


Fields

  • private System.Diagnostics.Stopwatch m_Stopwatch Used internally to measure elapsed time for barrier execution or profiling/debug UI. Modders can rely on the barrier to contain such internal timing helpers (not intended for external access).

  • private Unity.Jobs.JobHandle <producerHandle>k__BackingField Backing field for the producerHandle property. This holds the combined JobHandle of producers that produced work for this barrier (so the barrier can depend on/complete them before playback).

Properties

  • public Unity.Jobs.JobHandle producerHandle { get; private set } Exposes the JobHandle produced by systems/jobs that enqueued commands through this barrier. The setter is private — the barrier system updates this when command buffers are created/used. Consumers can read this handle to establish dependencies (for example, schedule follow-up jobs that must wait for producers to finish).

Constructors

  • public EndFrameBarrier() Default constructor. The barrier is registered as a managed system with the world; typical initialization is performed in OnCreate.

Methods

  • protected virtual OnCreate() : System.Void Called during system creation. Typical tasks performed here are: calling base.OnCreate(), initializing internal state such as the stopwatch, and obtaining/initializing any resources the barrier needs. The barrier also integrates with the world so CreateCommandBuffer calls return command buffers that will be played back at the appropriate end-of-frame point.

Additional common method (not listed above) - CreateCommandBuffer() : EntityCommandBuffer (typical usage) Creates and returns an EntityCommandBuffer (or an equivalent command buffer wrapper) that records entity structural changes. The returned command buffer is tied to the barrier and will be played back by the barrier at the end of the frame (or at the barrier's sync point). When scheduling jobs that write to the command buffer, the resulting JobHandle should be combined into the barrier's producerHandle so the barrier knows to wait for job completion before playback.

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // Internal profiling timer used by the barrier implementation
    m_Stopwatch = new System.Diagnostics.Stopwatch();
}

// Typical usage in other systems:
var endFrameBarrier = World.DefaultGameObjectInjectionWorld.GetOrCreateSystemManaged<EndFrameBarrier>();
var ecb = endFrameBarrier.CreateCommandBuffer();
// use ecb to record structural changes (CreateEntity, AddComponent, SetComponent, DestroyEntity, etc.)
// if you schedule jobs that produce work for this ECB, make sure to capture and chain the JobHandle
// into endFrameBarrier.producerHandle (or let the barrier API do it for you) so playback waits for jobs.

Notes for modders: - Use the barrier's CreateCommandBuffer to safely enqueue structural changes from systems and jobs. - If scheduling jobs that write to the command buffer, ensure proper JobHandle chaining so playback runs after producers finish. - Do not rely on internal fields (like m_Stopwatch or backing fields) — use the public API (CreateCommandBuffer and producerHandle) where possible.