Game.GenerateNotificationsSystem
Assembly:
Game (inferred from file path)
Namespace: Game.Tools
Type:
class GenerateNotificationsSystem
Base:
GameSystemBase
Summary:
GenerateNotificationsSystem is a Unity ECS system used by the Cities: Skylines 2 modding code to convert creation/definition entities into runtime notification icon entities. It queries for entities with CreationDefinition (and Updated) and optionally IconDefinition, then schedules a Burst-compiled IJobChunk (GenerateIconsJob) to process matching chunks in parallel. The job reads CreationDefinition and IconDefinition, resolves prefab references and NotificationIconData metadata, creates notification icon entities using a ModificationBarrier1 command buffer (parallel writer), and attaches the appropriate components (PrefabRef, Icon, Temp, DisallowCluster, Hidden) depending on CreationFlags and available archetypes. If the NotificationIconData lacks a specific archetype, the system uses a default archetype created at OnCreate. The system sets itself to require the matching query for updates.
Fields
-
private ModificationBarrier1 m_ModificationBarrier
Used to obtain a command buffer (AsParallelWriter) for creating and mutating entities from the scheduled job, and to register job handles produced by that buffer. -
private EntityQuery m_DefinitionQuery
EntityQuery that matches entities with CreationDefinition and Updated, and optionally IconDefinition. The system calls RequireForUpdate with this query so the system runs only when matching entities exist. -
private EntityArchetype m_DefaultArchetype
A fallback archetype created in OnCreate for notification icons that lack a valid archetype in NotificationIconData. Created as an archetype withPrefabRef
andIcon
components. -
private TypeHandle __TypeHandle
Holds ComponentTypeHandle and ComponentLookup handles for the job: CreationDefinition (RO), IconDefinition (RO), PrefabRef (RO lookup), NotificationIconData (RO lookup). Populated via __AssignHandles when the system is prepared for execution. -
(Nested)
GenerateIconsJob
(BurstCompile)
The IJobChunk that performs the chunk-by-chunk processing. Its fields include ComponentTypeHandle/Lookup values, the default archetype, and an EntityCommandBuffer.ParallelWriter for entity creation. -
(Nested)
TypeHandle
(struct)
Encapsulates the ComponentTypeHandle and ComponentLookup fields and provides __AssignHandles(ref SystemState) to retrieve them from the SystemState.
Properties
- None (this system does not expose public properties)
Constructors
public GenerateNotificationsSystem()
Default parameterless constructor. Marked with [Preserve] in the source to avoid stripping.
Methods
protected override void OnCreate()
Initializes the system:- Retrieves the ModificationBarrier1 system instance from the World and stores it in m_ModificationBarrier.
- Builds m_DefinitionQuery to match entities with
CreationDefinition
andUpdated
, and optionallyIconDefinition
. - Creates m_DefaultArchetype as an archetype with
PrefabRef
andIcon
components. -
Calls RequireForUpdate(m_DefinitionQuery) so the system runs only when there are matching entities.
-
protected override void OnUpdate()
Schedules the Burst-compiled GenerateIconsJob as a parallel JobChunk over m_DefinitionQuery: - Fills the job struct with component handles/lookups (via InternalCompilerInterface.GetComponentTypeHandle/GetComponentLookup using the cached __TypeHandle), the default archetype, and a parallel command buffer from m_ModificationBarrier.
- Schedules the job with JobChunkExtensions.ScheduleParallel and the current base.Dependency.
-
Registers the resulting job handle with m_ModificationBarrier via AddJobHandleForProducer, and assigns the scheduled handle to base.Dependency.
-
private void __AssignQueries(ref SystemState state)
Compiler helper used during OnCreateForCompiler — currently creates and disposes a temporary EntityQueryBuilder (no runtime behavior beyond initialization in the compiled code). -
protected override void OnCreateForCompiler()
Compiler-time initialization: calls __AssignQueries and __TypeHandle.__AssignHandles to set up query handles and component type/lookup handles in the compiled environment. -
(Nested)
GenerateIconsJob.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
Core job execution logic: - Reads arrays of CreationDefinition and IconDefinition from the chunk.
- For each element:
- Builds an Icon component from IconDefinition (location, priority, cluster layer, flags).
- Builds a PrefabRef from CreationDefinition.m_Prefab, but if CreationDefinition.m_Original has a PrefabRef, uses that prefab instead.
- Skips processing if prefab is null or there's no NotificationIconData for the prefab.
- Retrieves NotificationIconData and ensures it has a valid archetype; if not and the creation is Permanent, skip; otherwise assign the default archetype.
- If CreationDefinition.m_Original is valid, add a Hidden component to that original entity (via the command buffer).
- Create a new entity using the notification icon archetype, then set its PrefabRef and Icon components.
- If the creation is not Permanent, attach a Temp component (with m_Original and flags mapped from CreationFlags to TempFlags: Essential, Delete, Select, Create) and DisallowCluster to the new entity.
-
Note: The job uses parallel command buffer indices (unfilteredChunkIndex) when creating and modifying entities.
-
(Nested)
void IJobChunk.Execute(...)
Explicit interface implementation that forwards to the strongly-typed Execute method above.
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// Typical initialization performed by GenerateNotificationsSystem:
m_ModificationBarrier = base.World.GetOrCreateSystemManaged<ModificationBarrier1>();
m_DefinitionQuery = GetEntityQuery(new EntityQueryDesc {
All = new ComponentType[] {
ComponentType.ReadOnly<CreationDefinition>(),
ComponentType.ReadOnly<Updated>()
},
Any = new ComponentType[] { ComponentType.ReadOnly<IconDefinition>() }
});
m_DefaultArchetype = base.EntityManager.CreateArchetype(
ComponentType.ReadWrite<PrefabRef>(),
ComponentType.ReadWrite<Icon>()
);
RequireForUpdate(m_DefinitionQuery);
}
Notes and implementation details: - The system heavily uses Unity's ECS low-level APIs: ComponentTypeHandle, ComponentLookup (previously ComponentDataFromEntity), EntityArchetype, EntityCommandBuffer.ParallelWriter, and JobChunk scheduling. - The job is annotated with [BurstCompile] for performance. - The system maps CreationFlags on CreationDefinition to TempFlags on Temp to indicate lifecycle intent (Permanent vs temporary, Delete/Select/Create). - The command buffer is obtained from ModificationBarrier1 to safely produce structural changes from the job and to correctly coordinate dependencies with the system's job.