Skip to content

Game.EndPrefabSerializationSystem

Assembly: Game (user assembly)
Namespace: Game.Serialization

Type: class

Base: GameSystemBase

Summary:
EndPrefabSerializationSystem is a managed ECS system used at the end of a prefab serialization step. It updates the game's SaveGameSystem.referencedContent list (an Entity array) with entities that contain content prefabs, and runs a burst-compiled JobChunk that synchronizes PrefabData.m_Index from the corresponding LoadedIndex buffer for loaded prefab entities. The job also updates the enabled mask for prefab components based on whether the prefab index is valid (>= 0). The system uses internal type-handle plumbing produced by the compiler to obtain BufferTypeHandle and ComponentTypeHandle instances for job scheduling.


Fields

  • private SaveGameSystem m_SaveGameSystem
    Holds a reference to the SaveGameSystem instance for the world. Used to read and update SaveGameSystem.referencedContent. Initialized in OnCreate via World.GetOrCreateSystemManaged().

  • private EntityQuery m_LoadedPrefabsQuery
    Query selecting entities that have the LoadedIndex buffer. Used to schedule the EndPrefabSerializationJob across chunks that contain loaded prefabs.

  • private EntityQuery m_ContentPrefabQuery
    Query selecting entities that have both ContentData and PrefabData components. Used to build the SaveGameSystem.referencedContent entity array.

  • private TypeHandle __TypeHandle
    Compiler-generated struct grouping BufferTypeHandle (read-only) and ComponentTypeHandle (read/write). It exposes a method to assign the handles from a SystemState and is used by OnCreateForCompiler / OnUpdate to populate the job's handle fields via InternalCompilerInterface.

  • (nested) private struct EndPrefabSerializationJob (Burst-compiled)
    Job struct implementing IJobChunk used to process prefab chunks. Contains the fields:

  • BufferTypeHandle<LoadedIndex> m_LoadedIndexType (ReadOnly) — buffer handle for LoadedIndex buffer.
  • ComponentTypeHandle<PrefabData> m_PrefabDataType — component handle for PrefabData (read/write). The job's Execute iterates chunk elements, reads the first LoadedIndex from the buffer accessor and writes it into PrefabData.m_Index, and updates the enabled mask for PrefabData based on whether the index is valid.

  • (nested) private struct TypeHandle
    Compiler-generated struct containing:

  • BufferTypeHandle<LoadedIndex> __Game_Prefabs_LoadedIndex_RO_BufferTypeHandle (ReadOnly)
  • ComponentTypeHandle<PrefabData> __Game_Prefabs_PrefabData_RW_ComponentTypeHandle Includes an aggressive-inlined __AssignHandles(ref SystemState) that calls state.GetBufferTypeHandle and state.GetComponentTypeHandle to populate these handles.

Properties

  • None (no public or protected properties exposed by this system).
    Note: the job uses internal type handles that are assigned from the system state and passed to the scheduled job via InternalCompilerInterface.GetBufferTypeHandle / GetComponentTypeHandle.

Constructors

  • public EndPrefabSerializationSystem()
    Default parameterless constructor. Marked with [Preserve] in the source to avoid stripping. The constructor itself does not perform initialization beyond what the base GameSystemBase constructor does — initialization (queries, system references) occurs in OnCreate.

Methods

  • protected override void OnCreate()
    Called when the system is created. Implementation:
  • Calls base.OnCreate().
  • Obtains the SaveGameSystem instance: m_SaveGameSystem = World.GetOrCreateSystemManaged().
  • Initializes two EntityQuery objects:
    • m_LoadedPrefabsQuery = GetEntityQuery(ComponentType.ReadOnly()) — used to schedule the chunk job.
    • m_ContentPrefabQuery = GetEntityQuery(ComponentType.ReadOnly(), ComponentType.ReadOnly()) — used to gather prefab content entities for save-game referencing.
  • This prepares the system for OnUpdate work.

  • protected override void OnUpdate()
    Main update logic executed every frame / system tick:

  • If m_SaveGameSystem.referencedContent.IsCreated is true, it disposes the existing NativeArray to avoid leaks.
  • Reassigns m_SaveGameSystem.referencedContent to the result of m_ContentPrefabQuery.ToEntityArray(Allocator.Persistent). This keeps a persistent list of content-prefab entities referenced by the save game system.
  • Schedules and completes a parallel JobChunk (EndPrefabSerializationJob) over m_LoadedPrefabsQuery. Prior to scheduling, it obtains the job's BufferTypeHandle and ComponentTypeHandle via InternalCompilerInterface.GetBufferTypeHandle / GetComponentTypeHandle with the stored __TypeHandle values and the system's CheckedStateRef. The job runs to completion (Complete()) on the same frame.
  • The job updates PrefabData.m_Index from the first element of the LoadedIndex buffer per entity and sets the enabled mask for PrefabData depending on whether m_Index >= 0.

  • private void __AssignQueries(ref SystemState state)
    Compiler-generated helper used by OnCreateForCompiler. Current implementation builds a temporary EntityQueryBuilder and disposes it; effectively a placeholder used by generated code paths. Marked MethodImplOptions.AggressiveInlining in the source for the TypeHandle method, but here it is simply called during compiler-time setup.

  • protected override void OnCreateForCompiler()
    Called by the compiled system wiring. It:

  • Calls base.OnCreateForCompiler().
  • Calls __AssignQueries(ref base.CheckedStateRef) to set up any compiler-handled queries.
  • Calls __TypeHandle.__AssignHandles(ref base.CheckedStateRef) to populate the cached BufferTypeHandle and ComponentTypeHandle instances so they can be used in OnUpdate (and be converted to job handles via InternalCompilerInterface).

  • (nested) EndPrefabSerializationJob.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask) : void
    Job execution logic:

  • Retrieves a NativeArray for the chunk using m_PrefabDataType.
  • Retrieves a BufferAccessor for the chunk using m_LoadedIndexType.
  • Gets an EnabledMask for PrefabData using chunk.GetEnabledMask(ref m_PrefabDataType).
  • Iterates through the elements in the chunk:
    • If enabledMask[i] is true, reads bufferAccessor[i][0].m_Index and assigns it to PrefabData.m_Index for that entity.
    • Otherwise preserves the existing PrefabData value.
    • After updating value, sets enabledMask[i] to (value.m_Index >= 0) — effectively disabling PrefabData if the index is negative.
  • This both writes the PrefabData component and adjusts the enabled mask for the PrefabData component in the chunk.
  • The struct also explicitly implements IJobChunk.Execute by forwarding to the above method.

  • (nested) TypeHandle.__AssignHandles(ref SystemState state)
    Assigns the internal handles:

  • __Game_Prefabs_LoadedIndex_RO_BufferTypeHandle = state.GetBufferTypeHandle(isReadOnly: true)
  • __Game_Prefabs_PrefabData_RW_ComponentTypeHandle = state.GetComponentTypeHandle() These are used later to create the job's m_LoadedIndexType and m_PrefabDataType via InternalCompilerInterface.

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // Initialize reference to save-game system and queries (matches source)
    m_SaveGameSystem = World.GetOrCreateSystemManaged<SaveGameSystem>();
    m_LoadedPrefabsQuery = GetEntityQuery(ComponentType.ReadOnly<LoadedIndex>());
    m_ContentPrefabQuery = GetEntityQuery(ComponentType.ReadOnly<ContentData>(), ComponentType.ReadOnly<PrefabData>());
}

// OnUpdate schedules a Burst job that copies LoadedIndex -> PrefabData.m_Index
[Preserve]
protected override void OnUpdate()
{
    // Dispose previous referencedContent if present, then rebuild the persistent array
    if (m_SaveGameSystem.referencedContent.IsCreated)
        m_SaveGameSystem.referencedContent.Dispose();

    m_SaveGameSystem.referencedContent = m_ContentPrefabQuery.ToEntityArray(Allocator.Persistent);

    // Schedule and complete the chunk job that syncs PrefabData.m_Index from LoadedIndex buffers
    var job = new EndPrefabSerializationJob
    {
        m_LoadedIndexType = InternalCompilerInterface.GetBufferTypeHandle(ref __TypeHandle.__Game_Prefabs_LoadedIndex_RO_BufferTypeHandle, ref base.CheckedStateRef),
        m_PrefabDataType = InternalCompilerInterface.GetComponentTypeHandle(ref __TypeHandle.__Game_Prefabs_PrefabData_RW_ComponentTypeHandle, ref base.CheckedStateRef)
    };
    JobChunkExtensions.ScheduleParallel(job, m_LoadedPrefabsQuery, base.Dependency).Complete();
}

Notes and cautions: - The job is Burst-compiled and uses internal handles — this pattern is produced by the compiler; avoid manually mutating internal handle fields. - The system disposes and reassigns a persistent NativeArray to SaveGameSystem.referencedContent every update, so ensure that any other code accessing that array respects its lifetime. The code protects against leaks by disposing if IsCreated. - EnabledMask usage toggles PrefabData enabled state based on whether the loaded index is valid (m_Index >= 0). This can affect other systems relying on component enablement.