Skip to content

Game.Prefabs.MovingObjectPrefab

Assembly: Assembly-CSharp
Namespace: Game.Prefabs

Type: class

Base: ObjectGeometryPrefab

Summary:
A prefab type for objects that can move. This prefab ensures the entity archetypes and component sets required for both moving and stopped states are created and stored on the prefab entity. It adds runtime components such as MovingObjectData and (transient) UpdateFrame, and when a Moving component is present it ensures transform-related components (TransformFrame and InterpolatedTransform) are included. RefreshArchetype builds two archetypes: one used while the object is moving and another used when it is stopped, and writes those archetypes into ObjectData and MovingObjectData on the prefab entity. This class is intended to be subclassed to add additional components or to customize archetype composition for specific moving objects.


Fields

  • None declared in this type.
    This class does not declare its own fields; it uses component data and archetype storage via EntityManager.SetComponentData for ObjectData and MovingObjectData when RefreshArchetype runs.

Properties

  • None declared in this type.
    There are no explicit properties defined on MovingObjectPrefab. Any relevant data is stored in component data types (ObjectData, MovingObjectData) on the prefab entity.

Constructors

  • public MovingObjectPrefab()
    Default parameterless constructor (compiler-provided). Use subclass constructors if you need to initialize managed state in derived prefabs. Typical prefab initialization occurs through the entity conversion / prefab creation flow rather than per-instance constructor logic.

Methods

  • public override void GetPrefabComponents(HashSet<ComponentType> components)
    Collects component types that should be present on the prefab entity itself. This override calls the base implementation and then adds MovingObjectData as a read/write component type. When creating or inspecting prefabs, this indicates the prefab will produce entities that need MovingObjectData.

  • public override void GetArchetypeComponents(HashSet<ComponentType> components)
    Contributes component types to archetypes used for runtime instances of the prefab. This override:

  • Calls the base implementation.
  • Adds UpdateFrame (read/write) to all runtime archetypes.
  • If the set of components already contains Moving (read/write), it also adds TransformFrame and InterpolatedTransform (read/write).
    This ensures that moving objects get the update and interpolation/transform components necessary for movement and smoothing.

  • protected override void RefreshArchetype(EntityManager entityManager, Entity entity)
    Builds and writes the actual archetypes used by this prefab to the prefab entity's component data. Implementation details:

  • Gathers the prefab's subcomponents via GetComponents(list) (inherited).
  • Creates a hash set seeded with Moving and asks each subcomponent to contribute their archetype components.
  • Adds Created and Updated and creates an archetype, which is stored in ObjectData.m_Archetype on the prefab entity via entityManager.SetComponentData.
  • Then creates a second hash set seeded with Stopped and asks each subcomponent to contribute their archetype components for the stopped state.
  • Adds Created and Updated and creates an archetype, which is stored in MovingObjectData.m_StoppedArchetype on the prefab entity.
    Notes:
  • This method uses EntityManager and must run in the appropriate thread/context for entity conversion or prefab initialization.
  • The produced archetypes are arrays generated via PrefabUtils.ToArray(hashSet).

Usage Example

// Example of extending MovingObjectPrefab to add a custom runtime component
public class MyVehiclePrefab : MovingObjectPrefab
{
    public override void GetPrefabComponents(HashSet<ComponentType> components)
    {
        base.GetPrefabComponents(components);
        // Ensure prefab includes a custom component type so instances get it
        components.Add(ComponentType.ReadWrite<MyVehicleData>());
    }

    public override void GetArchetypeComponents(HashSet<ComponentType> components)
    {
        base.GetArchetypeComponents(components);
        // Add any extra runtime-only components the moving object needs
        components.Add(ComponentType.ReadWrite<MyVehicleRuntimeFlags>());
    }
}

Notes and tips: - RefreshArchetype creates two archetypes: one associated with ObjectData.m_Archetype (the "moving" archetype seeded with Moving) and one stored in MovingObjectData.m_StoppedArchetype (seeded with Stopped). Systems that instantiate moving objects should use these archetypes appropriately depending on object state. - Because the class relies on component types like Moving, Stopped, UpdateFrame, TransformFrame and InterpolatedTransform, ensure those component structs/types exist and are registered in the project. - The prefab lifecycle typically runs during conversion or prefab registration; avoid calling RefreshArchetype at runtime on a live world unless you understand the threading and entity manager context.