Game.Prefabs.LotInitializeSystem
Assembly: Game
Namespace: Game.Prefabs
Type: class
Base: GameSystemBase
Summary:
LotInitializeSystem is an ECS system used to initialize per-lot geometry information for lot prefabs. It scans each prefab's SubObject buffers and any placeholder object elements to compute the maximum object height (AreaGeometryData.m_MaxHeight) used by lot area geometry. Work is performed in a Burst-compiled IJobChunk (InitializeLotPrefabsJob) and scheduled in parallel for the matching prefab entities (Created + PrefabData + LotData + AreaGeometryData). The system uses ComponentTypeHandle/BufferTypeHandle and ComponentLookup/BufferLookup patterns for efficient, job-friendly access.
Fields
-
private PrefabSystem m_PrefabSystem
Used to access prefab-related systems from the World. Assigned in OnCreate via World.GetOrCreateSystemManaged(). This is a managed reference used outside the job. -
private EntityQuery m_PrefabQuery
EntityQuery selecting entities that represent created lot prefabs requiring AreaGeometryData initialization. The query matches entities with Created, PrefabData, LotData and AreaGeometryData components. The system requires this query for updates (RequireAnyForUpdate). -
private TypeHandle __TypeHandle
Container for the ComponentTypeHandle/BufferTypeHandle/ComponentLookup/BufferLookup used by the job. The nested TypeHandle struct provides an __AssignHandles method that initializes handles from a SystemState; this is a common pattern in generated Unity ECS code to cache type handles for job scheduling. -
private struct InitializeLotPrefabsJob
(nested)
Burst-compiled IJobChunk that performs the actual initialization. Contains: - BufferTypeHandle
m_SubObjectType (read-only) - ComponentTypeHandle
m_AreaGeometryType (read/write) - ComponentLookup
m_ObjectGeometryData (read-only) -
BufferLookup
m_PlaceholderObjectElements (read-only)
The job iterates chunks, resets AreaGeometryData.m_MaxHeight to 0, iterates SubObject buffers, skips edge-placed subobjects, and queries either placeholder object entries or the subobject prefab's ObjectGeometryData to compute the maximum mesh/top Y bound. -
private struct TypeHandle
(nested)
Holds the actual handles used by __TypeHandle.__AssignHandles (BufferTypeHandle, ComponentTypeHandle, ComponentLookup, BufferLookup). This struct's __AssignHandles is called from OnCreateForCompiler to initialize handles from the system state.
Properties
- No public properties.
All relevant data access is via the job's handles and internal fields; the system exposes no public properties.
Constructors
public LotInitializeSystem()
Default constructor. The class is annotated with [Preserve] on lifecycle methods and constructor to ensure Unity's managed code stripping doesn't remove it. Construction is managed by the World/Systems manager.
Methods
protected override void OnCreate()
Called when the system is created. Responsibilities:- Acquire the PrefabSystem instance from the World: m_PrefabSystem = base.World.GetOrCreateSystemManaged
(); - Create the EntityQuery to select created lot prefabs: GetEntityQuery(ComponentType.ReadOnly
(), ComponentType.ReadOnly (), ComponentType.ReadOnly (), ComponentType.ReadWrite ()); - Call RequireAnyForUpdate(m_PrefabQuery) so the system only runs when matching entities exist.
Method is decorated with [Preserve].
-
protected override void OnUpdate()
Schedules InitializeLotPrefabsJob as a parallel JobChunk over m_PrefabQuery. It constructs the job with the current type/lookup handles retrieved through InternalCompilerInterface.Get* on the cached TypeHandle fields and schedules it with JobChunkExtensions.ScheduleParallel. The resulting JobHandle is stored in base.Dependency. This is the runtime execution path that updates AreaGeometryData.m_MaxHeight for matching prefab entities. -
private void __AssignQueries(ref SystemState state)
A small helper used by generated/compiled code to assign or initialize EntityQuery builders. In this implementation it creates and immediately disposes an EntityQueryBuilder(Allocator.Temp) — a placeholder pattern used in compiled ECS code. -
protected override void OnCreateForCompiler()
Called by generated code tooling; ensures __AssignQueries(ref base.CheckedStateRef) is called and that the cached __TypeHandle.__AssignHandles(ref base.CheckedStateRef) is performed so type handles are set up correctly before job scheduling. This method helps the ahead-of-time compiled system to work with the ECS compiler/runtime. -
InitializeLotPrefabsJob.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
Core job logic: - Get native array of AreaGeometryData for the chunk and a BufferAccessor for SubObject buffers.
- For each entity in the chunk:
- Reset value.m_MaxHeight = 0f.
- If a SubObject buffer exists, iterate every SubObject:
- Skip subobjects flagged with EdgePlacement.
- If the SubObject's prefab has PlaceholderObjectElement buffer entries, iterate those placeholder entries and look up each placeholder m_Object's ObjectGeometryData; use its bounds.max.y to update m_MaxHeight (math.max).
- Else if the SubObject's prefab itself has ObjectGeometryData, use its bounds.max.y to update m_MaxHeight.
- Save the updated AreaGeometryData back into the native array.
-
This job is Burst-compiled and uses ComponentLookup/BufferLookup to fetch ObjectGeometryData and placeholder buffers in a job-safe way.
-
Explicit interface wrapper: IJobChunk.Execute(...)
The job also provides the explicit interface implementation that simply forwards to the strongly-typed Execute method.
Usage Example
This system runs automatically as part of the game's ECS world, but a minimal illustrative relevant snippet (mirrors the system's OnCreate) showing how this system initializes its query and prefabs reference:
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// Acquire PrefabSystem for any managed prefab operations.
m_PrefabSystem = base.World.GetOrCreateSystemManaged<PrefabSystem>();
// Query: created lot prefabs which have area geometry to initialize.
m_PrefabQuery = GetEntityQuery(
ComponentType.ReadOnly<Created>(),
ComponentType.ReadOnly<PrefabData>(),
ComponentType.ReadOnly<LotData>(),
ComponentType.ReadWrite<AreaGeometryData>());
// Only run this system when matching entities exist.
RequireAnyForUpdate(m_PrefabQuery);
}
Additional notes for modders: - If you add new subobject prefabs or placeholder objects, ensure they carry ObjectGeometryData components describing bounds; LotInitializeSystem relies on those to compute m_MaxHeight. - The job is Burst-compiled and scheduled in parallel; avoid adding managed object access or non-blittable types in the data paths it touches. - To force re-initialization after changing prefab data at runtime, update/Create/mark matching entities so the Created + PrefabData + LotData + AreaGeometryData query sees them (or modify the system update conditions). - The system uses internal compiler interface patterns and TypeHandle caching; when authoring custom systems following the same pattern, ensure proper use of GetComponentTypeHandle/GetBufferTypeHandle and ComponentLookup/BufferLookup for job safety and performance.