Skip to content

Game.TriggerPrefab

Assembly: Assembly-CSharp.dll
Namespace: Game.Prefabs

Type: class

Base: PrefabBase

Summary:
TriggerPrefab is a Prefab asset used to configure in-game triggers. It exposes a TriggerType, a set of optional PrefabBase references that will be associated with the trigger, and TargetType flags that define what kinds of objects the trigger should affect. During prefab initialization this class writes one or more TriggerData entries into the entity's TriggerData dynamic buffer so runtime systems can process trigger behaviour.


Fields

  • public TriggerType m_TriggerType
    The type of trigger (enum defined in Game.Triggers). Determines the trigger behaviour applied to created TriggerData entries (for example Enter, Exit, Stay — consult Game.Triggers.TriggerType for exact values).

  • public PrefabBase[] m_TriggerPrefabs
    Optional array of PrefabBase references. If set, each non-null referenced prefab will be converted to an entity reference and an associated TriggerData entry will be added for it in LateInitialize. If null or empty, a single TriggerData entry without a linked prefab entity is added.

  • [EnumFlag] public TargetType m_TargetTypes
    Flags enum (defined in Game.Triggers) that indicates which target types the trigger applies to (for example Vehicle, Pedestrian, Building). The EnumFlag attribute allows multiple values to be selected.

Properties

  • (none declared in this class)
    This class does not declare public properties. It relies on public fields for serialized configuration and overrides methods from PrefabBase for runtime initialization.

Constructors

  • public TriggerPrefab()
    No explicit constructor is declared in the source; the default parameterless constructor is used. Initialization of runtime data is performed in LateInitialize when the prefab is instantiated into an entity.

Methods

  • public override void GetDependencies(List<PrefabBase> prefabs)
    Adds referenced prefabs from m_TriggerPrefabs to the provided dependencies list. This ensures that any prefabs referenced by this TriggerPrefab are known dependencies and can be loaded/initialized before this prefab.

Behaviour details: - Calls base.GetDependencies(prefabs). - If m_TriggerPrefabs is null, returns early. - Iterates m_TriggerPrefabs and adds non-null entries to prefabs.

  • public override void GetPrefabComponents(HashSet<ComponentType> components)
    Advertises that this prefab requires TriggerData on the entity. It calls the base implementation and then adds ComponentType.ReadWrite() to the provided components set. This ensures the ECS entity created for this prefab has a DynamicBuffer.

  • public override void LateInitialize(EntityManager entityManager, Entity entity)
    Performs runtime initialization on the created entity. Key steps:

  • Calls the base LateInitialize.
  • Obtains the managed PrefabSystem from the entity manager's world.
  • Retrieves the DynamicBuffer for the entity.
  • If m_TriggerPrefabs is non-null and non-empty, iterates each referenced prefab and, for each non-null entry, adds a TriggerData item with:
    • m_TriggerType = this.m_TriggerType
    • m_TargetTypes = this.m_TargetTypes
    • m_TriggerPrefab = existingSystemManaged.GetEntity(prefabBase) (the entity corresponding to the referenced PrefabBase)
  • If m_TriggerPrefabs is null or empty, adds a single TriggerData with m_TriggerType and m_TargetTypes (no linked prefab entity).
  • Result: entity's TriggerData buffer contains one or more entries describing triggers to be used by trigger-processing systems.

Notes: - This method relies on PrefabSystem.GetEntity to convert PrefabBase references into entity handles. Ensure the referenced prefabs are present in the PrefabSystem (GetDependencies helps with that). - LateInitialize is called as part of prefab finalization — do not call it manually unless you understand prefab initialization ordering.

Usage Example

// Example: manually adding a TriggerData entry to an entity's buffer (similar to what TriggerPrefab does)
protected void ExampleAddTrigger(EntityManager entityManager, Entity entity, PrefabBase referencedPrefab)
{
    var prefabSystem = entityManager.World.GetExistingSystemManaged<PrefabSystem>();
    var buffer = entityManager.GetBuffer<TriggerData>(entity);

    // Add an entry that links to a referenced prefab entity
    buffer.Add(new TriggerData
    {
        m_TriggerType = TriggerType.Enter,
        m_TargetTypes = TargetType.Vehicle | TargetType.Pedestrian,
        m_TriggerPrefab = prefabSystem.GetEntity(referencedPrefab) // can be Entity.Null if not linking to prefab
    });
}

Additional tips: - Configure m_TriggerType, m_TargetTypes and m_TriggerPrefabs in the prefab asset (inspector) when authoring mods. - Ensure referenced prefabs are included in dependencies so PrefabSystem can resolve them to entities during LateInitialize. - Consult Game.Triggers.TriggerType and Game.Triggers.TargetType enums for available trigger types and target flags.