Skip to content

Game.Prefabs.Destruction

Assembly: Assembly-CSharp (inferred)
Namespace: Game.Prefabs

Type: class

Base: ComponentBase

Summary:
Prefab component used to mark an event prefab that triggers a "destruction" event in the game's event system. When a prefab with this component is converted to an entity, the component writes a DestructionData component to the entity containing the configured target selection mode and occurrence probability. The class also contributes archetype components used by the runtime event systems (a Game.Events.Destruction tag/struct and a TargetElement component).


Fields

  • public EventTargetType m_RandomTargetType
    Defines how the event chooses its target(s). This is an enum (EventTargetType) used by the event system to select the target for the destruction event (for example, a random building, district, or player-defined target). This value is copied into the DestructionData component during Initialize.

  • public float m_OccurenceProbability = 0.01f
    Probability that this event will occur when considered by the event scheduler. Default value is 0.01 (1%). This value is copied into the DestructionData component during Initialize.

Properties

  • This class defines no properties.

Constructors

  • public Destruction()
    Default parameterless constructor (implicit). Instances of this component are typically created and configured in the Unity editor as part of an Event prefab.

Methods

  • public override void GetPrefabComponents(HashSet<ComponentType> components)
    Called during conversion to entity to declare which component types should be added to the prefab entity. This implementation adds a read/write DestructionData component type so that the component data struct exists on entities created from the prefab.

  • public override void GetArchetypeComponents(HashSet<ComponentType> components)
    Called to contribute extra component types to the entity archetype. This implementation adds Game.Events.Destruction (a runtime event marker/struct) and TargetElement (used to store computed or runtime target information) as read/write components to the archetype.

  • public override void Initialize(EntityManager entityManager, Entity entity)
    Called after the entity is created (conversion/initialization). This method builds a DestructionData struct from the serialized inspector fields (m_RandomTargetType and m_OccurenceProbability) and writes it to the entity via entityManager.SetComponentData. It also calls base.Initialize(entityManager, entity) first to allow ComponentBase initialization behavior.

Usage Example

// Example: reading data after conversion/initialization in a system or initialization callback
using Unity.Entities;
using UnityEngine;

public class DestructionDebugSystem : ComponentSystem
{
    protected override void OnUpdate()
    {
        Entities.ForEach((ref DestructionData data) =>
        {
            Debug.Log($"Destruction target type: {data.m_RandomTargetType}, probability: {data.m_OccurenceProbability}");
        });
    }
}

// Alternatively, inside an Initialize override for a custom prefab component you control:
protected override void Initialize(EntityManager entityManager, Entity entity)
{
    base.Initialize(entityManager, entity);
    // Values written by the Destruction prefab component are now available on the entity
    var destruction = entityManager.GetComponentData<DestructionData>(entity);
    Debug.Log($"Configured destruction probability: {destruction.m_OccurenceProbability}");
}