Skip to content

Game.Serialization.AttachmentSystem

Assembly:
Namespace: Game.Serialization

Type: class

Base: GameSystemBase

Summary:
AttachmentSystem is a small ECS system that finds entities with an Attached component and updates the corresponding parent entity's Attachment component so the parent record points to the attached child. It schedules a Burst-compiled IJobChunk (AttachmentJob) which reads entities and Attached components and writes into a ComponentLookup to set Attachment.m_Attached on the parent entity. The class is compiler-generated and uses Unity's Entities job APIs and type handles to build and schedule the job each frame.


Fields

  • private EntityQuery m_Query
    Used to query entities that have the Attached component. The query is created in OnCreate and also used with RequireForUpdate to make the system only run when such entities exist.

  • private TypeHandle __TypeHandle
    Container for the EntityTypeHandle, the read-only ComponentTypeHandle, and the read/write ComponentLookup. These handles are assigned from the SystemState in OnCreateForCompiler via __TypeHandle.__AssignHandles and are later converted to runtime handles used by the job via InternalCompilerInterface in OnUpdate.

Additional (nested) runtime data: - The nested AttachmentJob contains: - EntityTypeHandle m_EntityType (read-only) — used to get Entity values from the chunk. - ComponentTypeHandle<Attached> m_AttachedType (read-only) — used to read Attached components from the chunk. - ComponentLookup<Attachment> m_AttachmentData (read/write) — used to read/update the Attachment component on the parent entity.

Properties

  • (none)
    This system exposes no public properties.

Constructors

  • public AttachmentSystem()
    Default constructor. Marked with [Preserve] so it's not stripped by managed code stripping. No custom constructor logic is present.

Methods

  • protected override void OnCreate()
    Creates the EntityQuery for Attached components:
  • m_Query = GetEntityQuery(ComponentType.ReadOnly());
  • RequireForUpdate(m_Query); This ensures the system only updates when there are entities with Attached.

  • protected override void OnUpdate()
    Builds an AttachmentJob instance by obtaining runtime handles through InternalCompilerInterface (converting the stored __TypeHandle handles into the job's EntityTypeHandle, ComponentTypeHandle, and ComponentLookup) and schedules it with JobChunkExtensions.Schedule(jobData, m_Query, base.Dependency). The job iterates chunks of entities with Attached and updates the parent entity's Attachment component to reference the attached child entity.

  • private void __AssignQueries(ref SystemState state)
    Compiler-side helper that currently creates and disposes an EntityQueryBuilder with Allocator.Temp. Present to satisfy generated code patterns; no query assignments are stored here.

  • protected override void OnCreateForCompiler()
    Compiler-time initialization helper: calls __AssignQueries(ref base.CheckedStateRef) and __TypeHandle.__AssignHandles(ref base.CheckedStateRef) to populate the generated type handles used by the system.

Nested/Job methods:

  • AttachmentJob.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
    For each entity in the chunk:
  • Read the Entity and its Attached component.
  • If the parent entity referenced by Attached has an Attachment component, read that Attachment, set its m_Attached field to the child Entity, and write it back using the ComponentLookup. This method is Burst-compiled via [BurstCompile] on the job struct and is scheduled as a chunk job.

  • TypeHandle.__AssignHandles(ref SystemState state)
    Assigns the generated EntityTypeHandle, read-only ComponentTypeHandle, and ComponentLookup from the provided SystemState. This ties the TypeHandle fields to the current world/system state so they can be converted at runtime for job usage.

Notes: - The class is marked [CompilerGenerated] and contains several [Preserve] annotations; this indicates the code is generated by the Entities compiler and intended to be kept by the linker. - The job uses ComponentLookup to perform read/write updates on parent entities from a worker job. Ensure proper safety and that Attachment exists on parent entities to avoid unexpected behavior.

Usage Example

// Typical usage: the system runs automatically once present in the world.
// Example showing how to create a parent and a child entity so the system updates the parent's Attachment:

var entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;

// Create archetypes / entities
Entity parent = entityManager.CreateEntity(typeof(Attachment));
Entity child = entityManager.CreateEntity(typeof(Attached));

// Initialize components
entityManager.SetComponentData(parent, new Attachment { m_Attached = Entity.Null }); // Attachment defined elsewhere
entityManager.SetComponentData(child, new Attached { m_Parent = parent });         // Attached defined elsewhere

// When AttachmentSystem runs (OnUpdate), it will set parent.Attachment.m_Attached = child

This system is intended to be used within Unity's DOTS/Entities runtime; you generally don't instantiate or call it manually—it's created and scheduled by the world/system bootstrap.