Game.ObjectPrefab
Assembly: Assembly-CSharp.dll
Namespace: Game.Prefabs
Type: class
Base: PrefabBase
Summary:
ObjectPrefab is a prefab definition class used by the game's ECS-based prefab system to represent "object" prefabs (in-game objects). It participates in two related responsibilities:
- Declaring which components belong to the prefab asset (via GetPrefabComponents).
- Constructing the runtime archetype for instantiated entities (via GetArchetypeComponents and RefreshArchetype) and storing that archetype in the ObjectData component so instances can be created with the correct component set.
It ensures that every object prefab includes the runtime components Game.Objects.Object and Transform and that the created archetype contains the Created and Updated marker components used by the game's systems.
Fields
- This class does not declare any private or public fields of its own in the provided source. It relies on methods inherited from PrefabBase and uses ECS components (ObjectData) to store runtime data such as the created archetype.
Properties
- This class does not declare any new properties in the provided source. It interacts with component data (ObjectData) on the prefab entity to store the generated archetype.
Constructors
public ObjectPrefab()
This class does not declare an explicit constructor in source; a default parameterless constructor is present implicitly and will call the base PrefabBase constructor. Typical initialization logic is performed in LateInitialize rather than a constructor, because prefab lifecycle is managed by the engine.
Methods
-
public override void GetPrefabComponents(HashSet<ComponentType> components)
Adds the component types that should be present on the prefab asset itself. Implementation calls base.GetPrefabComponents(components) and then adds ComponentType.ReadWrite(). ObjectData on the prefab entity is used to store runtime-specific information (notably the created archetype). -
public override void GetArchetypeComponents(HashSet<ComponentType> components)
Adds the minimal set of components that every runtime instance of this prefab must have. This implementation adds Game.Objects.Object and Transform (both ReadWrite). It also calls the base implementation so other inherited component types are included. -
public override void LateInitialize(EntityManager entityManager, Entity entity)
Called late in prefab initialization. The override calls the base LateInitialize and then calls RefreshArchetype to build and assign the runtime archetype for this prefab entity. Doing archetype creation during initialization ensures this work is not repeated per-instance at runtime. -
protected virtual void RefreshArchetype(EntityManager entityManager, Entity entity)
Builds the runtime archetype for the prefab and stores it on the prefab's ObjectData component: - Calls GetComponents to collect all ComponentBase instances that belong to this prefab (these are the modular components on the prefab asset).
- For each collected component, calls component.GetArchetypeComponents(hashSet) to collect the component types required by that component at runtime.
- Adds Created and Updated marker components (ComponentType.ReadWrite
() and ComponentType.ReadWrite ()) to the set. - Creates an archetype from the union of component types via entityManager.CreateArchetype(...) and assigns it to ObjectData.m_Archetype using entityManager.SetComponentData(entity, new ObjectData { m_Archetype = ... }).
Notes:
- PrefabUtils.ToArray(hashSet) is used to convert the HashSet
Usage Example
// Example: a simple custom prefab subclass that ensures the base behavior runs.
// You typically don't need to call RefreshArchetype yourself; the engine calls LateInitialize.
public class MyObjectPrefab : ObjectPrefab
{
public override void GetPrefabComponents(HashSet<ComponentType> components)
{
base.GetPrefabComponents(components);
// Add any extra component types that this prefab asset needs at the prefab level.
}
public override void GetArchetypeComponents(HashSet<ComponentType> components)
{
base.GetArchetypeComponents(components);
// Add any extra runtime components required on instances of this prefab.
}
}
Additional tips: - If you add custom ComponentBase-derived components to the prefab asset, ensure those components implement GetArchetypeComponents so RefreshArchetype includes their runtime component types. - Because archetype creation and assignment happens in LateInitialize / RefreshArchetype, prefer doing heavy setup there; per-instance instantiation should then be fast (instantiation simply uses the pre-built archetype). - The Created and Updated marker components are commonly used by game systems to drive initialization and change tracking for new/modified entities.