Game.Prefabs.CityModifiers
Assembly: Assembly-CSharp (game assembly)
Namespace: Game.Prefabs
Type: class
Base: ComponentBase
Summary:
Component intended to be attached to policy prefabs (see ComponentMenu attribute "Policies/"). It exposes an array of CityModifierInfo entries (m_Modifiers) which are converted into CityModifierData entries on the Entity when the prefab is initialized. This component ensures the prefab requests a DynamicBuffer
Fields
public CityModifierInfo[] m_Modifiers
Array of modifier definitions that will be applied to the city. Each CityModifierInfo instance provides the values (type, mode, range) that are converted into CityModifierData entries and added to the entity's DynamicBuffer during Initialize. This field is serialized/exposed on the prefab component so designers can configure multiple modifiers in the inspector.
Properties
- This type does not define any public properties.
Constructors
public CityModifiers()
No explicit constructor is defined in the source; the default parameterless constructor provided by C# / Unity is used. Initialization logic is implemented in the overridden Initialize method called by the prefab/system lifecycle.
Methods
-
public override void GetPrefabComponents(HashSet<ComponentType> components)
AddsComponentType.ReadWrite<CityModifierData>()
to the provided set. This tells the prefab/archtype system that the prefab requires a DynamicBufferon the entity so that Initialize can write modifier entries into that buffer. -
public override void GetArchetypeComponents(HashSet<ComponentType> components)
No archetype components are added here. The method is intentionally empty because modifiers are stored in a dynamic buffer rather than a fixed structural component. -
public override void Initialize(EntityManager entityManager, Entity entity)
Called when the prefab instance is being initialized. Calls the base implementation then, ifm_Modifiers
is not null, gets theDynamicBuffer<CityModifierData>
from the entity and iterates them_Modifiers
array. For eachCityModifierInfo cityModifierInfo
it adds a newCityModifierData(cityModifierInfo.m_Type, cityModifierInfo.m_Mode, cityModifierInfo.m_Range)
to the buffer. This populates the entity with the configured city modifiers.
Notes:
- The code assumes a CityModifierData
buffer exists on the entity (ensured by GetPrefabComponents).
- If Initialize is invoked multiple times for the same entity, callers should be aware it will append entries again (the class does not deduplicate or clear the buffer).
Usage Example
// Example: configure the prefab component (can be set in the inspector)
// This sets up one modifier on the prefab; when the prefab is instantiated
// Initialize will add the corresponding CityModifierData into the entity's buffer.
public class ExampleSetup
{
void Configure(GameObject prefab)
{
var cm = prefab.GetComponent<Game.Prefabs.CityModifiers>();
if (cm == null) cm = prefab.AddComponent<Game.Prefabs.CityModifiers>();
cm.m_Modifiers = new[]
{
new CityModifierInfo { m_Type = CityModifierType.TaxRate, m_Mode = CityModifierMode.Add, m_Range = 0.05f },
new CityModifierInfo { m_Type = CityModifierType.Happiness, m_Mode = CityModifierMode.Set, m_Range = 1.0f },
};
}
}
// After the prefab is instantiated and Initialize runs, you can inspect the buffer:
DynamicBuffer<CityModifierData> buffer = entityManager.GetBuffer<CityModifierData>(entity);
foreach (var md in buffer)
{
UnityEngine.Debug.Log($"Modifier: {md.Type} Mode:{md.Mode} Range:{md.Range}");
}