Game.UI.InGame.NotificationsSection
Assembly: Game (Game.dll)
Namespace: Game.UI.InGame
Type: class
Base: InfoSectionBase
Summary:
NotificationsSection is an InfoSectionBase-derived UI backend used by the in-game info panel to collect, cache and expose notification icons attached to an entity (and related entities such as workers, renters, visitors, route waypoints and citizens visiting a building). It uses Unity's ECS API and Jobs (including Burst-compiled jobs) to gather notification data off the main thread into Native containers, then converts that data into managed NotificationInfo objects for display and serialization. The section is configured to display for destroyed objects, outside connections, constructions and upgrades. It manages NativeList/NativeArray resources and must dispose them when destroyed.
Fields
-
private ImageSystem m_ImageSystem
Used to fetch icon textures (and placeholder icon) for prefabs. Initialized in OnCreate by retrieving the ImageSystem from the World. -
private EntityQuery m_CitizenQuery
An EntityQuery built in OnCreate that selects entities with CurrentBuilding and IconElement components (excluding Deleted and Temp). Used to schedule the visitor-checking job (IJobChunk). -
private NativeList<Notification> m_NotificationsResult
NativeList used as a temporary collection filled by the jobs (CheckAndCacheNotificationsJob and CheckAndCacheVisitorNotificationsJob). Holds Notification structs produced from Icon/PrefabRef combination. Allocated in OnCreate and disposed in OnDestroy. -
private NativeArray<bool> m_DisplayResult
Single-element NativeArray that acts as a boolean flag written by the jobs to indicate whether any notification should be displayed. Allocated in OnCreate and disposed in OnDestroy. -
private TypeHandle __TypeHandle
Internal struct used to cache ComponentLookup/BufferLookup/TypeHandle instances required by the jobs. Assigned via __AssignHandles in OnCreateForCompiler. Contains both read-only and read-write handles for PrefabRef, Icon, IconElement buffers, Citizen, Employee, Renter, HouseholdCitizen, RouteWaypoint, CurrentBuilding and the EntityTypeHandle. -
private List<NotificationInfo> notifications { get; set; }
Managed list of NotificationInfo objects used by the UI to represent grouped notifications per entity. Populated during OnProcess based on contents of m_NotificationsResult, merged by entity and sorted. Initialized in OnCreate. -
Nested types (jobs and type handle)
CheckAndCacheNotificationsJob
(BurstCompile, IJob) — scans a selected entity's own IconElement buffer and related buffers (Employee, Renter -> HouseholdCitizen, RouteWaypoint) to collect notifications into m_NotificationResult and mark m_DisplayResult true if any found. Uses ComponentLookup/BufferLookup passed in from __TypeHandle via InternalCompilerInterface.CheckAndCacheVisitorNotificationsJob
(BurstCompile, IJobChunk) — iterates chunks of citizen entities (those that have CurrentBuilding and IconElement) and collects notifications for citizens currently visiting the selected building.TypeHandle
— contains ComponentLookup/BufferLookup/ComponentTypeHandle/EntityTypeHandle fields and an __AssignHandles method to initialize them from a SystemState.
Properties
-
protected override string group => "NotificationsSection"
Identifier string used by the InfoSectionBase infrastructure to group this section. -
protected override bool displayForDestroyedObjects => true
Indicates the section should be visible for destroyed objects. -
protected override bool displayForOutsideConnections => true
Indicates the section should be visible for outside connection entities. -
protected override bool displayForUnderConstruction => true
Indicates the section should be visible for entities under construction. -
protected override bool displayForUpgrades => true
Indicates the section should be visible for upgrading entities. -
private List<NotificationInfo> notifications { get; set; }
Managed property holding the aggregated NotificationInfo list used by the UI and serialized in OnWriteProperties.
Constructors
public NotificationsSection()
Default constructor (empty). Primary initialization occurs in OnCreate (and OnCreateForCompiler) rather than the constructor.
Methods
-
protected override void Reset()
Clears the managed notifications list, clears the native m_NotificationsResult list, and resets the m_DisplayResult flag to false. Called by the InfoSectionBase lifecycle to reset state. -
[Preserve] protected override void OnCreate()
Initial setup: acquires ImageSystem from the World, builds the m_CitizenQuery, allocates m_DisplayResult (NativeArraywith length 1, Allocator.Persistent), allocates m_NotificationsResult (NativeList , Allocator.Persistent), and creates the managed notifications List . Note: __TypeHandle.__AssignHandles is called from OnCreateForCompiler; OnCreate is focused on runtime allocations and query creation. -
[Preserve] protected override void OnDestroy()
Disposes native containers (m_NotificationsResult and m_DisplayResult) and then calls base.OnDestroy(). Always ensure native Allocator.Persistent containers are disposed to avoid leaks. -
[Preserve] protected override void OnUpdate()
Primary data-gathering step run each update: - Schedules and completes CheckAndCacheNotificationsJob (IJob) for the selectedEntity to gather notifications from the entity itself and its related buffers (employees, renters, route waypoints, household citizens).
- Schedules and completes CheckAndCacheVisitorNotificationsJob (IJobChunk) over m_CitizenQuery to gather notifications from visiting citizens whose CurrentBuilding equals selectedEntity.
- After both jobs complete, reads m_DisplayResult[0] and sets base.visible accordingly. Notes:
- Both jobs are Burst-compiled and use ComponentLookup/BufferLookup handles obtained via InternalCompilerInterface/TypeHandle.
-
Jobs write into native m_NotificationsResult and set m_DisplayResult[0] if anything was found.
-
protected override void OnProcess()
Converts the NativeListresults into managed NotificationInfo objects: - Iterates m_NotificationsResult and for each Notification creates a NotificationInfo and either merges it into an existing NotificationInfo (if same entity) or appends a new one.
- For Building entities, AddTarget is called to aggregate multiple targets per building.
-
Finally sorts the notifications list. This step runs on the main thread and prepares managed data for UI rendering and serialization.
-
public static bool HasNotifications(Entity entity, BufferLookup<IconElement> iconBuffer, ComponentLookup<Icon> iconDataFromEntity)
Static helper that checks if an entity has any IconElements whose referenced Icon component is present and not of cluster layer Marker. Returns true if at least one valid icon exists in the buffer. Used by jobs to early-out or set the display flag. -
public static NativeList<Notification> GetNotifications(EntityManager EntityManager, Entity entity, NativeList<Notification> notifications)
Static helper overload that reads IconElement buffer and Icon/PrefabRef components using an EntityManager (managed API) to append Notification entries into the provided NativeList. Useful for managed code paths needing the same notification extraction behavior. -
public static NativeList<Notification> GetNotifications(Entity entity, ComponentLookup<PrefabRef> prefabRefDataFromEntity, ComponentLookup<Icon> iconDataFromEntity, BufferLookup<IconElement> iconBufferFromEntity, NativeList<Notification> notifications)
ECS-friendly overload used by the Burst jobs: reads the IconElement buffer via BufferLookup and for each Icon entity uses ComponentLookupand ComponentLookup to construct and append Notification entries into the provided NativeList. -
public override void OnWriteProperties(IJsonWriter writer)
Serializes the managed notifications list to JSON for the UI (or external tools): - Writes a "notifications" array with an entry for each NotificationInfo.
- For each notification item writes "key" (prefab name from m_PrefabSystem), "count" (notification count), and "iconPath" (icon path resolved via ImageSystem.GetIcon(prefab) or a placeholder fallback). Notes:
- This method depends on m_PrefabSystem (from base) and m_ImageSystem.
-
Prefab lookup is attempted with m_PrefabSystem.TryGetPrefab
. -
[MethodImpl(MethodImplOptions.AggressiveInlining)] private void __AssignQueries(ref SystemState state)
Compiler helper invoked from OnCreateForCompiler; here it creates/initializes any query builders required by the generated code. In this implementation it only constructs and disposes a temporary EntityQueryBuilder — __TypeHandle.__AssignHandles handles the main handle assignments. -
protected override void OnCreateForCompiler()
Compiler / code-gen helper that calls __AssignQueries and invokes __TypeHandle.__AssignHandles to initialize component lookup/type handles from the passed SystemState (base.CheckedStateRef). Required so the generated code has valid handles for job scheduling. -
private struct TypeHandle.__AssignHandles(ref SystemState state)
Initializes ComponentLookup, BufferLookup , ComponentTypeHandle and EntityTypeHandle instances from the provided SystemState. Must be called before using the cached handles in jobs (done by OnCreateForCompiler).
Notes on Jobs and Burst: - CheckAndCacheNotificationsJob and CheckAndCacheVisitorNotificationsJob are decorated with [BurstCompile], meaning they can run with Burst for high performance. They use only the ComponentLookup/BufferLookup/TypeHandle handles passed to them, and write into m_NotificationResult and m_DisplayResult. - Jobs are scheduled from OnUpdate and immediately Complete()'d — this design performs the heavy work in parallelizable code but waits for completion before continuing (so UI code can safely read results).
Usage Example
// Example showing the typical pattern used in NotificationsSection.OnCreate and lifecycle.
// This snippet mirrors what NotificationsSection does: get ImageSystem, build queries,
// allocate natives and initialize managed lists.
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// Get image system for icons
m_ImageSystem = base.World.GetOrCreateSystemManaged<ImageSystem>();
// Build query for visitors: entities with CurrentBuilding + IconElement, excluding Deleted/Temp
m_CitizenQuery = GetEntityQuery(new EntityQueryDesc
{
All = new ComponentType[] {
ComponentType.ReadOnly<CurrentBuilding>(),
ComponentType.ReadOnly<IconElement>()
},
None = new ComponentType[] {
ComponentType.ReadOnly<Deleted>(),
ComponentType.ReadOnly<Temp>()
}
});
// Allocate persistent native containers (must be disposed in OnDestroy)
m_DisplayResult = new NativeArray<bool>(1, Allocator.Persistent);
m_NotificationsResult = new NativeList<Notification>(10, Allocator.Persistent);
// Managed list holding processed NotificationInfo objects for UI
notifications = new List<NotificationInfo>(10);
}
Notes for modders: - If you create custom Icon/PrefabRef components or custom IconElement semantics, ensure your icons' Icon.m_ClusterLayer is set appropriately; Marker-layer icons are ignored by this system. - Because this section uses persistent Native containers, always call base.OnDestroy or override OnDestroy and dispose any additional native memory you create. - If you extend or schedule similar jobs, be careful to provide correct ComponentLookup/BufferLookup handles obtained from the current SystemState (see TypeHandle.__AssignHandles usage pattern). - The jobs are completed synchronously in OnUpdate (Complete() invoked). If you want asynchronous behavior, you must manage Dependencies and lifetime carefully and adapt OnProcess to only run once jobs have completed.