Game.Prefabs.AnimalPrefab
Assembly:
Namespace: Game.Prefabs
Type: class
Base: CreaturePrefab
Summary:
AnimalPrefab is a prefab component used to configure animal-type creatures. It exposes editable movement parameters (m_MoveSpeed and m_Acceleration) and ensures the necessary ECS component types are added to the prefab and the runtime archetype. During initialization it converts the inspector move speed (stored in km/h) to internal units (m/s) and writes those values into the AnimalData component on the entity.
Fields
-
public float m_MoveSpeed = 20f
Editable move speed for the animal. In the prefab/inspector this value is expressed in km/h; Initialize converts it to m/s by dividing by 3.6 and stores it into the AnimalData component. -
public float m_Acceleration = 10f
Editable acceleration for the animal (unit is consistent with runtime usage). This value is copied into AnimalData during Initialize.
Properties
- None declared on AnimalPrefab itself. (Inherited properties from CreaturePrefab are available depending on that base class.)
Constructors
public AnimalPrefab()
Default parameterless constructor (implicit). The fields m_MoveSpeed and m_Acceleration are initialized to their default values declared above.
Methods
-
public override void GetPrefabComponents(HashSet<ComponentType> components)
Adds component types required on the prefab itself. This override calls base.GetPrefabComponents then adds AnimalData as a read/write component type so the prefab includes AnimalData. -
public override void GetArchetypeComponents(HashSet<ComponentType> components)
Adds runtime archetype component types required for animal entities. Calls base.GetArchetypeComponents and then adds: - Animal (ReadWrite)
- AnimalNavigation (ReadWrite)
- Target (ReadWrite)
-
Blocker (ReadWrite) These ensure animal entities are created with the correct ECS components.
-
public override void Initialize(EntityManager entityManager, Entity entity)
Performs runtime initialization for an instantiated entity. It: - Calls base.Initialize(entityManager, entity).
- Reads the AnimalData component for the entity.
- Converts the prefab m_MoveSpeed from km/h to m/s: componentData.m_MoveSpeed = m_MoveSpeed / 3.6f.
- Copies m_Acceleration into componentData.m_Acceleration.
- Writes the modified AnimalData back to the entity using entityManager.SetComponentData.
Usage Example
// Typical usage: configure prefab values in inspector or at runtime,
// then when the prefab is used to create an entity Initialize will copy/convert them.
//
// Example: tweak prefab values in code before entity creation
AnimalPrefab prefab = /* obtain reference to the AnimalPrefab instance */;
prefab.m_MoveSpeed = 36f; // 36 km/h (will be converted to 10 m/s at initialization)
prefab.m_Acceleration = 5f;
// When the prefab system creates an entity from this prefab, AnimalPrefab.Initialize
// will be invoked and AnimalData on the entity will receive the converted values.
Notes: - m_MoveSpeed is specified in km/h on the prefab and converted to m/s by Initialize (division by 3.6). - The class relies on Unity.Entities.ComponentType and EntityManager to add and set ECS component data.