Skip to content

Game.Prefabs.Modes.ZonePollutionGlobalMode

Assembly:
Assembly-CSharp (typical Unity game assembly; actual assembly may vary)
Namespace:
Game.Prefabs.Modes

Type:
class

Base:
EntityQueryModePrefab

Summary:
ZonePollutionGlobalMode is a prefab-mode component used to adjust pollution values (ground, air, noise) for zone buildings at the ECS level. It defines three multipliers that are applied to each matched entity's PollutionData via a Burst-compiled IJobChunk (ModeJob). The class stores original PollutionData for each affected entity so changes can be restored later. The mode targets entities that have SpawnableBuildingData, BuildingSpawnGroupData and PollutionData components (i.e., zone buildings), and operates by scheduling a parallel JobChunk to multiply the pollution values.


Fields

  • public float m_GroundPollutionMultiplier
    Multiplier applied to PollutionData.m_GroundPollution for matched entities. Default value is set in the prefab/inspector.

  • public float m_AirPollutionMultiplier
    Multiplier applied to PollutionData.m_AirPollution for matched entities. Default value is set in the prefab/inspector.

  • public float m_NoisePollutionMultiplier
    Multiplier applied to PollutionData.m_NoisePollution for matched entities. Default value is set in the prefab/inspector.

  • private System.Collections.Generic.Dictionary<Unity.Entities.Entity, PollutionData> m_OriginalPollutionData
    Runtime dictionary storing the original PollutionData for each entity handled by the mode. Used by RestoreDefaultData to revert changes. This dictionary is created and accessed on the main thread (not thread-safe); it is populated in StoreDefaultData and read in RestoreDefaultData.

  • (Nested) private struct ModeJob : IJobChunk
    Burst-compiled job used to apply the multipliers to chunks of PollutionData. It contains fields for the three multipliers and a ComponentTypeHandle for writing pollution data. The job iterates each element in a chunk and multiplies ground/air/noise pollution values accordingly.

Properties

  • This class exposes no public C# properties.

Constructors

  • public ZonePollutionGlobalMode()
    No explicit constructor is defined in the source; the default constructor is used. Initialization of multipliers typically happens via prefab/inspector or serialized values.

Methods

  • public override EntityQueryDesc GetEntityQueryDesc()
    Returns an EntityQueryDesc that selects entities with:
  • ReadOnly
  • ReadOnly
  • ReadOnly This effectively targets zone buildings that contain pollution data.

  • protected override void RecordChanges(EntityManager entityManager, Unity.Entities.Entity entity)
    Currently implemented as a single call to entityManager.GetComponentData(entity). Likely used to mark that the mode will read/record the pollution component for that entity (used by the prefab/mode system to detect changes).

  • public override void StoreDefaultData(EntityManager entityManager, ref Unity.Collections.NativeArray<Unity.Entities.Entity> entities, PrefabSystem prefabSystem)
    Creates and populates m_OriginalPollutionData with the PollutionData of each entity in the provided entities array. After storing data it disposes the passed-in NativeArray. This allows later restoration of original pollution values.

  • public override JobHandle ApplyModeData(EntityManager entityManager, EntityQuery requestedQuery, JobHandle deps)
    Schedules and returns a JobHandle for a Burst-compiled ModeJob using JobChunkExtensions.ScheduleParallel. The ModeJob is created with the three multipliers and a writable ComponentTypeHandle obtained from the EntityManager. The scheduled job multiplies each entity's pollution fields by the configured multipliers in parallel.

  • public override void RestoreDefaultData(EntityManager entityManager, ref Unity.Collections.NativeArray<Unity.Entities.Entity> entities, PrefabSystem prefabSystem)
    Iterates the provided entities and attempts to restore each entity's PollutionData from m_OriginalPollutionData. If an entity is not present in the dictionary, it adds the current PollutionData to the dictionary (ensuring the map contains an entry). This method performs restoration on the main thread using EntityManager.SetComponentData.

  • (Nested) ModeJob.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
    Implementation of IJobChunk that:

  • Obtains a NativeArray for the chunk via the provided ComponentTypeHandle.
  • Loops over chunk.Count entries and multiplies value.m_GroundPollution, value.m_AirPollution and value.m_NoisePollution by the corresponding multipliers.
  • Writes the modified PollutionData back to the native array. The job is marked with [BurstCompile] for performance.

Notes and caveats: - The multiplier fields are public and expected to be set via prefab/inspector. Changing them at runtime requires ensuring the mode's ApplyModeData is called again to schedule the update job. - m_OriginalPollutionData is not thread-safe and is only used on the main thread; jobs themselves only work with component data and do not touch this dictionary. - The job schedules parallel writes to PollutionData — the job uses a non-read-only ComponentTypeHandle, so ensure no conflicting jobs modify the same data concurrently.

Usage Example

// Example pseudocode: store defaults, apply mode, then restore later.

ZonePollutionGlobalMode mode = /* obtained from prefab or component */;
mode.m_GroundPollutionMultiplier = 0.5f; // reduce ground pollution by 50%
mode.m_AirPollutionMultiplier = 1.0f;    // keep air pollution unchanged
mode.m_NoisePollutionMultiplier = 0.8f;  // reduce noise by 20%

// 1) When enabling the mode (run on main thread)
NativeArray<Entity> entities = /* query result or provided by prefab system */;
mode.StoreDefaultData(entityManager, ref entities, prefabSystem);

// 2) Apply the multipliers (schedules Burst IJobChunk)
EntityQuery query = entityManager.CreateEntityQuery(mode.GetEntityQueryDesc());
JobHandle deps = default;
deps = mode.ApplyModeData(entityManager, query, deps);
// Ensure to complete or integrate deps into the game's job chain as appropriate.

// 3) When disabling the mode, restore original pollution values (main thread)
NativeArray<Entity> entitiesToRestore = /* same entities as before or refetched */;
mode.RestoreDefaultData(entityManager, ref entitiesToRestore, prefabSystem);

If you need, I can also generate a short example that integrates this mode in a sample system or show how to query entities to obtain the NativeArray passed to StoreDefaultData/RestoreDefaultData.