Game.Prefabs.Pollution
Assembly: Assembly-CSharp
Namespace: Game.Prefabs
Type: class
Base: ComponentBase, IServiceUpgrade
Summary:
Provides prefab-level pollution settings for buildings. The component exposes ground, air and noise pollution values (non-negative) and an option to scale pollution with renters (household members/employees). At prefab initialization it converts these inspector values into a PollutionData ECS component and registers the required ComponentType for archetypes and prefabs. The class is decorated with a ComponentMenu attribute so it appears under Buildings in the editor.
Fields
-
public int m_GroundPollution
Ground pollution value emitted by the building. Has a [Min(0f)] attribute; must be non-negative. Used to populate PollutionData.m_GroundPollution. -
public int m_AirPollution
Air pollution value emitted by the building. Has a [Min(0f)] attribute; must be non-negative. Used to populate PollutionData.m_AirPollution. -
public int m_NoisePollution
Noise pollution value emitted by the building. Has a [Min(0f)] attribute; must be non-negative. Used to populate PollutionData.m_NoisePollution. -
public bool m_ScaleWithRenters
Controls whether the pollution values are multiplied by the number of renters (household members or employees). Tooltip indicates you can disable this if you don't want scaling. Default: true. Used to populate PollutionData.m_ScaleWithRenters.
Properties
- None declared directly on this class. (The component exposes its data via the GetPollutionData() method and through the ECS PollutionData component.)
Constructors
public Pollution()
Default parameterless constructor (implicit). Instances are typically created by the Unity editor when the component is added to a prefab.
Methods
-
public override void GetPrefabComponents(HashSet<ComponentType> components)
Adds the ECS component type required by this prefab to the provided set. Specifically, it adds ComponentType.ReadWrite() so entities spawned from this prefab include PollutionData. -
public override void GetArchetypeComponents(HashSet<ComponentType> components)
When creating archetypes for the prefab, this method will add the archetype components produced by GetPollutionData(), but only if the prefab does not have a ServiceUpgrade or PlaceholderBuilding component. This prevents upgrade/placeholder prefabs from automatically adding pollution archetype components. -
public void GetUpgradeComponents(HashSet<ComponentType> components)
Implements IServiceUpgrade: used to add pollution-related archetype components for upgrade prefabs (calls GetPollutionData().AddArchetypeComponents(components)). -
public override void Initialize(EntityManager entityManager, Entity entity)
Called when the prefab is instantiated into an ECS entity. It writes the PollutionData generated from this component's inspector fields into the provided entity using entityManager.SetComponentData(entity, GetPollutionData()). -
private PollutionData GetPollutionData()
Creates and returns a PollutionData struct populated from this component's fields (m_GroundPollution, m_AirPollution, m_NoisePollution, m_ScaleWithRenters). This is used by both archetype registration and entity initialization.
Usage Example
// Example: A building prefab with the Pollution component will automatically
// write a PollutionData component on entity creation. The following is
// illustrative of how Initialize is used by the prefab system.
// Inside the prefab component (no change required — the engine calls Initialize):
public override void Initialize(EntityManager entityManager, Entity entity)
{
// Writes PollutionData to the entity using the values set in the inspector.
entityManager.SetComponentData(entity, GetPollutionData());
}
// If you need to inspect PollutionData at runtime on an entity:
var pollution = entityManager.GetComponentData<PollutionData>(someEntity);
Debug.Log($"Ground: {pollution.m_GroundPollution}, Air: {pollution.m_AirPollution}, Noise: {pollution.m_NoisePollution}, ScalesWithRenters: {pollution.m_ScaleWithRenters}");
Notes: - The component is intended to be added to building prefabs (ComponentMenu "Buildings/"). - Ensure pollution values are non-negative (Min attribute enforces this in the editor). - The class integrates with the ECS workflow by registering and setting the PollutionData component on entities.