Skip to content

Game.Prefabs.Modes.PollutionModifierMode

Assembly: Game
Namespace: Game.Prefabs.Modes

Type: public class

Base: LocalModePrefab

Summary:
PollutionModifierMode is a prefab component used by mode prefabs to modify pollution-related multipliers (ground, air, noise) for one or more target prefabs at runtime via the ECS world. It stores an array of ModeData entries that reference a target PrefabBase and the three multiplier values. The component provides three main lifecycle methods: RecordChanges (to touch/validate / ensure the ECS component exists), ApplyModeData (to write the configured multipliers into the PollutionModifierData ECS component on the target entity), and RestoreDefaultData (to reset the ECS component values back to the prefab's original PollutionModifier values). The class relies on the presence of a PollutionModifier MonoBehaviour on the target PrefabBase and the PollutionModifierData ECS component on the corresponding entity.


Fields

  • public ModeData[] m_ModeDatas
    Array of ModeData entries. Each entry links a target PrefabBase to pollution multiplier values (ground, air, noise). Configured in the prefab inspector; iterated by RecordChanges/ApplyModeData/RestoreDefaultData.

  • public class ModeData (nested)

  • public PrefabBase m_Prefab
    Prefab reference whose PollutionModifier component and corresponding ECS entity will be targeted.
  • public float m_GroundPollutionMultiplier
    Ground pollution multiplier for this target. Attribute Range(-1f, 1f) limits values in the inspector.
  • public float m_AirPollutionMultiplier
    Air pollution multiplier for this target. Attribute Range(-1f, 1f).
  • public float m_NoisePollutionMultiplier
    Noise pollution multiplier for this target. Attribute Range(-1f, 1f).

Notes: - The array may be empty; methods iterate safely by length. - The PrefabBase must contain a PollutionModifier MonoBehaviour; otherwise the code logs a critical message and skips that entry.

Properties

  • None declared on this class.

Constructors

  • public PollutionModifierMode()
    Default parameterless constructor provided by Unity. Instances are typically created/serialized by the Unity engine when the prefab is loaded.

Methods

  • public override void RecordChanges(Unity.Entities.EntityManager entityManager, PrefabSystem prefabSystem)
    Iterates m_ModeDatas and for each entry:
  • Retrieves the PollutionModifier component from the referenced PrefabBase.
  • If missing, logs a critical error via ComponentBase.baseLog and continues.
  • Obtains the ECS Entity for the referenced prefab via prefabSystem.GetEntity(component.prefab).
  • Calls entityManager.GetComponentData(entity) — this effectively ensures the component exists / touches it (and may throw if the component type is missing on the entity). Purpose: validate and "record" the presence of the PollutionModifierData on the target entities so subsequent mode application works reliably.

  • public override void ApplyModeData(Unity.Entities.EntityManager entityManager, PrefabSystem prefabSystem)
    For each ModeData:

  • Gets the PollutionModifier MonoBehaviour from the referenced PrefabBase; logs a critical message and skips if missing.
  • Resolves the ECS Entity for the prefab.
  • Reads the existing PollutionModifierData from the entity, updates its m_GroundPollutionMultiplier, m_AirPollutionMultiplier, and m_NoisePollutionMultiplier fields from the ModeData values, and writes the updated struct back with entityManager.SetComponentData(entity, componentData). Effect: applies the configured multipliers to the running ECS component for the target prefab so the mode takes effect.

  • public override void RestoreDefaultData(Unity.Entities.EntityManager entityManager, PrefabSystem prefabSystem)
    For each ModeData:

  • Gets the PollutionModifier MonoBehaviour from the referenced PrefabBase; logs and skips if missing.
  • Obtains the ECS Entity for the prefab.
  • Reads the PollutionModifierData from the entity, then restores the multiplier fields to the values present on the PollutionModifier MonoBehaviour instance (component.m_GroundPollutionMultiplier, component.m_AirPollutionMultiplier, component.m_NoisePollutionMultiplier).
  • Writes the restored values back to the entity with SetComponentData. Effect: reverts any changes previously applied by ApplyModeData back to the prefab defaults.

Behavioral notes: - All three methods use prefabSystem.GetEntity(component.prefab) — they assume the PrefabSystem maps the referenced prefab to a valid ECS Entity. - If the PollutionModifierData component is not present on the entity, GetComponentData or SetComponentData will throw; RecordChanges appears intended to touch the component early (and may reveal missing components). - Logging uses ComponentBase.baseLog.Critical to report missing targets, making it clear which prefabs are misconfigured.

Usage Example

// Example: configure and apply a simple mode at runtime (pseudo-usage)
var mode = /* get PollutionModifierMode instance from a loaded mode prefab */;
mode.m_ModeDatas = new PollutionModifierMode.ModeData[] {
    new PollutionModifierMode.ModeData {
        m_Prefab = somePrefabBase,                // must have PollutionModifier MonoBehaviour
        m_GroundPollutionMultiplier = 0.5f,
        m_AirPollutionMultiplier = 0.8f,
        m_NoisePollutionMultiplier = -0.2f
    }
};

// When applying the mode:
mode.RecordChanges(entityManager, prefabSystem);    // validate / touch components
mode.ApplyModeData(entityManager, prefabSystem);    // apply configured multipliers

// To revert the changes:
mode.RestoreDefaultData(entityManager, prefabSystem);

Additional tips: - Ensure the target PrefabBase has a PollutionModifier MonoBehaviour and the ECS world has a PollutionModifierData component on the prefab entity before calling ApplyModeData. - Values are clamped in the inspector by the Range attributes but are not programmatically clamped in code — validate values if you set them from scripts.