Game.Prefabs.BuildingModules
Assembly: Assembly-CSharp (typical Unity game assembly; may vary per build)
Namespace: Game.Prefabs
Type: public class BuildingModules
Base: ComponentBase
Summary:
Component attached to building prefabs that references one or more module prefabs (m_Modules). During prefab initialization this component registers required prefab component types and, in LateInitialize, resolves each referenced PrefabBase to its Entity, ensures the module entity has a BuildingModuleData component and appends a BuildingModule entry to the parent building's BuildingModule buffer. The class is decorated with [ComponentMenu("Buildings/", typeof(BuildingPrefab))], so it is intended to be used on building prefab assets in the editor.
Fields
public PrefabBase[] m_Modules
An array of module prefabs referenced by this building prefab. Populated in the prefab (inspector). Used by GetDependencies to report prefab dependencies and by LateInitialize to attach module entities to the building's BuildingModule buffer.
Properties
- This class declares no public properties.
Constructors
public BuildingModules()
Default constructor (no custom initialization logic in source). Instances are normally created/serialized by Unity when the component is added to a prefab.
Methods
-
public override void GetDependencies(List<PrefabBase> prefabs)
Adds all entries from m_Modules to the provided prefabs list (after calling base.GetDependencies). Guarded by a null check on m_Modules. Use: ensures the prefab system knows these module prefabs are required by this building prefab. -
public override void GetPrefabComponents(HashSet<ComponentType> components)
Registers the required runtime component type for prefabs: ComponentType.ReadWrite(). This indicates that entities created from the prefab will use the BuildingModule buffer/component. -
public override void GetArchetypeComponents(HashSet<ComponentType> components)
Overrides the method but contains no implementation (no additional archetype components are added here). -
public override void Initialize(EntityManager entityManager, Entity entity)
Calls base.Initialize(entityManager, entity). No additional initialization logic is implemented in this override. -
public override void LateInitialize(EntityManager entityManager, Entity entity)
Main runtime hookup: if m_Modules is null the method returns early. Otherwise it gets the PrefabSystem from the world, iterates each PrefabBase in m_Modules, resolves its Entity, ensures the module entity has a BuildingModuleData component (entityManager.AddComponent(moduleEntity)) and adds a new BuildingModule (wrapping moduleEntity) to the BuildingModule buffer on the building prefab entity (entityManager.GetBuffer (entity).Add(new BuildingModule(moduleEntity))). Effectively attaches module entities to the parent building entity so that runtime systems can treat modules as part of the building.
Notes and edge cases:
- Respects null entries in the m_Modules array (skips null prefab entries).
- Uses entityManager.World.GetExistingSystemManaged
Usage Example
// Typical behavior is automatic during prefab instantiation. Example: how the component
// ensures module entities are associated with the parent building entity during LateInitialize.
protected override void LateInitialize(EntityManager entityManager, Entity entity)
{
base.LateInitialize(entityManager, entity);
// m_Modules populated in prefab inspector
if (m_Modules == null) return;
var prefabSystem = entityManager.World.GetExistingSystemManaged<PrefabSystem>();
for (int i = 0; i < m_Modules.Length; i++)
{
var modulePrefab = m_Modules[i];
if (modulePrefab == null) continue;
Entity moduleEntity = prefabSystem.GetEntity(modulePrefab);
entityManager.AddComponent<BuildingModuleData>(moduleEntity);
entityManager.GetBuffer<BuildingModule>(entity).Add(new BuildingModule(moduleEntity));
}
}
If you want to add module prefabs to a building in the editor, assign PrefabBase references to the m_Modules array on the BuildingModules component of the building prefab. During prefab instantiation the component will wire up module entities automatically.