Skip to content

Game.EndFrameBarrier

Assembly: Assembly-CSharp
Namespace: Game

Type: class

Base: Unity.Entities.BarrierSystem

Summary:
EndFrameBarrier is a synchronization/system helper used by the game to coordinate work that must finish at the end of a frame. It exposes a producerHandle JobHandle so producers can record their scheduled jobs and consumers (or the runtime) can combine or complete them at the appropriate point. The system also keeps a Stopwatch (m_Stopwatch) for simple timing/diagnostics to measure how long end-of-frame work takes (useful for profiling mod behavior). This class is intended for modders who need to schedule jobs that must be ensured complete before the next frame or before certain end-of-frame operations run.


Fields

  • private System.Diagnostics.Stopwatch m_Stopwatch
    Used to measure the duration of end-of-frame processing for diagnostics or profiling. The stopwatch is typically initialized in OnCreate and started/stopped around the work you want to measure. Keep in mind Stopwatch calls incur overhead — you may want to conditionally enable them only in debugging/profiling builds.

  • private Unity.Jobs.JobHandle <producerHandle>k__BackingField
    Backing field for the producerHandle property. This holds a JobHandle representing jobs scheduled by producers that should be considered part of end-of-frame work. Systems or components that schedule jobs at the end of a frame should combine their handles into this value (e.g., via JobHandle.CombineDependencies) so the barrier owns the dependency chain and can ensure completion at the appropriate time.

Properties

  • public Unity.Jobs.JobHandle producerHandle { get; private set }
    The JobHandle produced by systems that schedule end-of-frame jobs. Producers should set or combine their JobHandles into this property so the barrier system can track and resolve dependencies. Consumers that need to wait on end-of-frame work can read this handle and call Complete() or combine it into their own JobHandle. The setter is private — typically only the barrier system itself (or derived systems) updates it.

Constructors

  • public EndFrameBarrier()
    Default constructor. When the system is constructed by the ECS world, OnCreate will be called to perform initialization (including creating/initializing the stopwatch). No special parameters are required to instantiate this system; it is normally created/managed by the game’s ECS bootstrap.

Methods

  • protected virtual OnCreate() : System.Void
    Called when the system is created. Override to initialize internal data (for example, instantiate and reset m_Stopwatch). A typical override should call base.OnCreate() first, then perform initialization. Use OnCreate to set up any required state so that the barrier is ready the first frame it runs.

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    m_Stopwatch = new Stopwatch();
}

Additional notes for modders: - When scheduling a job that must be completed by end of frame, combine its JobHandle into EndFrameBarrier.producerHandle rather than completing it immediately on the main thread — this preserves asynchrony and helps performance. - To ensure all end-of-frame work is finished before performing a main-thread-only operation, call EndFrameBarrier.producerHandle.Complete() (or combine with other handles and complete). - Use the stopwatch for debug builds only; avoid leaving expensive timing active in release builds to reduce overhead. - Respect thread-safety rules of Unity.Jobs: only manipulate JobHandles from safe contexts and avoid capturing UnityEngine objects in jobs.