Game.Prefabs.BuildingModifiers
Assembly: Assembly-CSharp (typical Unity game assembly)
Namespace: Game.Prefabs
Type: public class
Base: ComponentBase
Summary:
BuildingModifiers is a prefab component used to convert a set of designer-specified BuildingModifierInfo entries into runtime BuildingModifierData entries on an entity. It is exposed in the editor under the Component menu "Policies/" (via the ComponentMenu attribute) and ensures the entity has a DynamicBuffer
Fields
public BuildingModifierInfo[] m_Modifiers
Array of designer-configured modifier descriptors (BuildingModifierInfo). During Initialize, each entry in this array is converted into a BuildingModifierData and added to the entity's DynamicBuffer. Can be null or empty.
Properties
- This class exposes no public properties.
Constructors
public BuildingModifiers()
Default constructor. Instances are typically created/serialized by Unity in the editor.
Methods
-
public override void GetPrefabComponents(HashSet<ComponentType> components)
Adds required component types for prefab conversion. Specifically, this method adds ComponentType.ReadWrite() so the prefab/entity archetype includes a DynamicBuffer . This ensures Initialize can safely obtain the buffer. -
public override void GetArchetypeComponents(HashSet<ComponentType> components)
Empty override. The class does not add additional archetype components beyond those added in GetPrefabComponents. -
public override void Initialize(EntityManager entityManager, Entity entity)
Called during prefab-to-entity initialization. Implementation: - Calls base.Initialize(entityManager, entity).
- If m_Modifiers is non-null, obtains DynamicBuffer
from the entity via entityManager.GetBuffer (entity). - Iterates the m_Modifiers array and adds a new BuildingModifierData for each BuildingModifierInfo using its fields (m_Type, m_Mode, m_Range). Notes:
- The presence of the DynamicBuffer is ensured by GetPrefabComponents.
- Each BuildingModifierInfo is translated into a BuildingModifierData by calling the BuildingModifierData constructor with buildingModifierInfo.m_Type, buildingModifierInfo.m_Mode, buildingModifierInfo.m_Range.
- If m_Modifiers is null or empty, no entries are added to the buffer.
Additional metadata: - Decorated with [ComponentMenu("Policies/", new Type[] { typeof(PolicyPrefab) })], which registers the component under the Policies/ menu and associates it with PolicyPrefab in the editor.
Usage Example
// Example: Accessing the modifiers that were added by BuildingModifiers.Initialize
Entity entity = /* entity created from a prefab that had BuildingModifiers */;
DynamicBuffer<BuildingModifierData> buffer = entityManager.GetBuffer<BuildingModifierData>(entity);
for (int i = 0; i < buffer.Length; i++)
{
BuildingModifierData mod = buffer[i];
// Inspect mod fields (type, mode, range) and apply logic in your system
}
{{ Notes and tips:
- Ensure BuildingModifierInfo and BuildingModifierData types are defined and compatible (the code assumes BuildingModifierInfo has m_Type, m_Mode, m_Range and BuildingModifierData has a constructor taking those values).
- Because GetPrefabComponents registers ComponentType.ReadWrite