Game.Prefabs.Modes.ServiceCoverageGlobalMode
Assembly: Assembly-CSharp
Namespace: Game.Prefabs.Modes
Type: class
Base: EntityQueryModePrefab
Summary:
ServiceCoverageGlobalMode is a mode prefab used to apply global multipliers to CoverageData components for entities that represent service coverage (ServiceCoverage prefabs). It schedules a Burst-compiled IJobChunk (ModeJob) to multiply range, capacity and magnitude values in parallel on the ECS chunks. The class also records and restores original CoverageData values so that mode changes can be reverted (using either previously stored data or values from the original prefab).
Fields
-
public float m_RangeMultiplier
Multiplies CoverageData.m_Range when the mode is applied. Typical values >1 increase range, values <1 decrease it. -
public float m_CapacityMultiplier
Multiplies CoverageData.m_Capacity when the mode is applied. -
public float m_MagnitudeMultiplier
Multiplies CoverageData.m_Magnitude when the mode is applied. -
private System.Collections.Generic.Dictionary<Unity.Entities.Entity, CoverageData> m_OriginalCoverageData
A dictionary storing original CoverageData for entities that had custom values at the time StoreDefaultData was called. Used by RestoreDefaultData to revert entities back to their original coverage if the mode is disabled. -
private struct ModeJob : IJobChunk
(nested)
The Burst compiled job that performs the per-chunk modification of CoverageData. See Methods for details.
Properties
- None (no public properties declared in this class)
Constructors
public ServiceCoverageGlobalMode()
Default constructor (the class relies on Unity/serialization for instantiation; no custom construction logic is implemented).
Methods
-
public override EntityQueryDesc GetEntityQueryDesc()
Returns the query used to select entities that the mode affects. This implementation returns a query that selects entities containing CoverageData as a read-only component (used to find relevant entities to operate on). -
protected override void RecordChanges(EntityManager entityManager, Entity entity)
Called to record that an entity was changed. This implementation simply reads the CoverageData component for the entity (entityManager.GetComponentData(entity)). The base mode system may use this to detect changes. -
public override void StoreDefaultData(EntityManager entityManager, ref NativeArray<Entity> entities, PrefabSystem prefabSystem)
Iterates provided entities and collects their CoverageData into m_OriginalCoverageData for entities that are ServiceCoverage prefabs (it checks the prefab via PrefabSystem and PrefabBase.TryGetExactly). This captures the current or custom values so they can be restored later. -
public override Unity.Jobs.JobHandle ApplyModeData(EntityManager entityManager, EntityQuery requestedQuery, Unity.Jobs.JobHandle deps)
Schedules the Burst-compiled ModeJob over the requested query with ScheduleParallel. The job is configured with the three multipliers and a writable ComponentTypeHandle. Returns the resulting JobHandle so the caller can chain dependencies. -
public override void RestoreDefaultData(EntityManager entityManager, ref NativeArray<Entity> entities, PrefabSystem prefabSystem)
Restores CoverageData for the supplied entities. For each entity: - If an original value exists in m_OriginalCoverageData, sets that CoverageData back on the entity.
- Else if the entity's prefab is a ServiceCoverage prefab, it restores m_Range, m_Capacity and m_Magnitude from the prefab's ServiceCoverage defaults.
- Else (no stored value and no matching prefab), it adds the current CoverageData into m_OriginalCoverageData (so future restores can use it).
ModeJob (nested struct) details:
- public float m_RangeMultiplier
- public float m_CapacityMultiplier
- public float m_MagnitudeMultiplier
- public ComponentTypeHandle<CoverageData> m_CoverageType
- public void Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
For each entity in the chunk, reads CoverageData, multiplies m_Range, m_Capacity and m_Magnitude by the configured multipliers, and writes the modified CoverageData back. The struct is decorated with [BurstCompile] for high-performance parallel execution. It implements IJobChunk.Execute explicitly to satisfy the job interface.
Notes on concurrency and correctness: - The job writes the CoverageData directly on entities (m_CoverageType is obtained with isReadOnly: false). Ensure that other systems modifying CoverageData are synchronized via JobHandle chaining. - m_OriginalCoverageData is an ordinary managed dictionary and is only used on the main thread (StoreDefaultData and RestoreDefaultData). The actual modification uses Burst jobs and native arrays.
Usage Example
// Typical usage is through the prefab/mode system in the game editor or when the PrefabSystem initializes modes.
// Example: configure multipliers on the mode instance (in editor or at runtime via script)
ServiceCoverageGlobalMode mode = /* obtain mode instance from prefab or component */;
mode.m_RangeMultiplier = 1.25f; // increase range by 25%
mode.m_CapacityMultiplier = 0.9f; // reduce capacity by 10%
mode.m_MagnitudeMultiplier = 1.0f; // leave magnitude unchanged
// When the mode system applies this mode it will call ApplyModeData and schedule the Burst job
// When disabling the mode, RestoreDefaultData will attempt to restore original values from stored data or prefab defaults.
If you want, I can also generate a short diagram of the data flow (StoreDefaultData -> ApplyModeData (Burst job) -> RestoreDefaultData) or provide a sample integration snippet showing how to obtain and enable this mode from the PrefabSystem.