Game.Prefabs.ArchetypePrefab
Assembly: Assembly-CSharp
Namespace: Game.Prefabs
Type: class
Base: PrefabBase
Summary:
ArchetypePrefab is a PrefabBase descendant used to construct and assign a Unity ECS Archetype for the entity backing this prefab. During initialization it collects component definitions from the prefab's component list, queries each component for the ECS components it requires for the archetype, adds framework lifecycle components (Created, Updated), builds an EntityArchetype, and stores that archetype in the entity's ArchetypeData component. This enables efficient instantiation and management of entities that share the same component layout.
Fields
- (none declared in this class)
This class does not declare private or public fields. It operates by calling methods on the base PrefabBase and interacting with ECS data via EntityManager.
Properties
- (none declared in this class)
No properties are declared. Archetype information is stored directly into the ArchetypeData component on the target entity.
Constructors
public ArchetypePrefab()
The class relies on the default parameterless constructor provided by C#. No custom construction logic is defined in this class.
Methods
-
public override void GetPrefabComponents(HashSet<ComponentType> components)
Adds the prefab-level components required by this prefab. This override calls the base implementation and then ensures the ArchetypeData component is included (ComponentType.ReadWrite), so the prefab entity will hold the archetype reference. -
public override void GetArchetypeComponents(HashSet<ComponentType> components)
Overrides the base method but does not add additional archetype components by itself. The method is available for derived prefabs to append archetype-level component types as needed. -
public override void LateInitialize(EntityManager entityManager, Entity entity)
Called after initial prefab setup. This override calls the base.LateInitialize and then calls RefreshArchetype to build and assign the entity archetype based on the prefab's current components. -
protected void RefreshArchetype(EntityManager entityManager, Entity entity)
Core logic for constructing the ECS archetype: - Collects the prefab's ComponentBase instances by calling GetComponents(list) (inherited from PrefabBase).
- For each component in the list, calls GetArchetypeComponents(hashSet) to aggregate all required ComponentType entries for the resulting archetype.
- Ensures lifecycle components are present by adding ComponentType.ReadWrite
() and ComponentType.ReadWrite (). - Converts the HashSet to an array (via PrefabUtils.ToArray(hashSet)), creates an EntityArchetype via entityManager.CreateArchetype(...), and writes the resulting archetype into the entity's ArchetypeData component using entityManager.SetComponentData(entity, archetypeData).
- This method centralizes archetype creation so any prefab with changes to its component list will rebuild the archetype when LateInitialize is invoked.
Usage Example
// Example: deriving from ArchetypePrefab to add a custom component to the prefab
public class MyBuildingPrefab : ArchetypePrefab
{
public override void GetPrefabComponents(HashSet<ComponentType> components)
{
base.GetPrefabComponents(components);
// Ensure the prefab includes MyBuildingData in its archetype
components.Add(ComponentType.ReadWrite<MyBuildingData>());
}
public override void LateInitialize(EntityManager entityManager, Entity entity)
{
base.LateInitialize(entityManager, entity);
// At this point RefreshArchetype has run and ArchetypeData on `entity` references
// an archetype that includes MyBuildingData along with Created and Updated.
// You can do any additional entity/component setup here if needed.
}
}
Notes and implementation tips: - ArchetypeData is the component used by this system to store the created EntityArchetype reference for the prefab entity. - Created and Updated components are added to the archetype to support the framework's lifecycle systems; do not remove them unless you understand the consequences. - If prefab components change (added/removed), ensure LateInitialize (or another appropriate point) is run again so RefreshArchetype recreates the archetype to match the new layout. - PrefabUtils.ToArray(hashSet) is used to produce the ComponentType[] required by EntityManager.CreateArchetype.