Skip to content

Game.Prefabs.TriggerPrefabData

Assembly: Game
Namespace: Game.Prefabs

Type: struct

Base: System.IDisposable

Summary:
TriggerPrefabData is a lightweight, job-friendly container used to map trigger definitions (trigger type + trigger prefab entity) to one or more prefab Entities that should be spawned/used for given target types. Internally it uses a NativeParallelMultiHashMap keyed by PrefabKey and stores PrefabValue entries. It is intended for use in Unity DOTS/Jobs contexts (uses Native containers and requires explicit disposal).


Fields

  • private NativeParallelMultiHashMap<PrefabKey, PrefabValue> m_PrefabMap
    Stores the mapping from PrefabKey (trigger type + trigger entity) to one or more PrefabValue entries (target types + prefab entity). Created with an initial capacity of 100 in the constructor and must be disposed via Dispose() to avoid leaks.

  • public struct PrefabKey

  • Fields:
    • public TriggerType m_TriggerType — the type of trigger (enum).
    • public Entity m_TriggerEntity — the trigger prefab entity that identifies the source prefab.
  • Behavior:

    • Implements IEquatable. Equality compares both trigger type and trigger entity.
    • GetHashCode is implemented as (int)m_TriggerType * 31 + m_TriggerEntity.GetHashCode().
  • public struct PrefabValue

  • Fields:

    • public TargetType m_TargetTypes — bitmask/flags indicating which target types this prefab applies to.
    • public Entity m_Prefab — the prefab entity to use for the matching trigger/target.
  • public struct Iterator

  • Fields:
    • public NativeParallelMultiHashMapIterator<PrefabKey> m_Iterator — iterator used to step through multi-hashmap values for a given key. Passed by ref between TryGetFirstPrefab and TryGetNextPrefab.

Properties

  • None.
    No public properties are exposed; all access is via methods.

Constructors

  • public TriggerPrefabData(Allocator allocator)
    Initializes the internal NativeParallelMultiHashMap with an initial capacity of 100 using the provided Allocator. Choose an appropriate allocator (for example Allocator.Persistent for long-lived containers). The created map must later be disposed via Dispose().

Methods

  • public void Dispose()
    Disposes the internal NativeParallelMultiHashMap to free native memory. Must be called when the TriggerPrefabData is no longer needed.

  • public void AddPrefab(Entity prefab, TriggerData triggerData)
    Adds a mapping from the trigger (type + trigger prefab entity found in triggerData) to a PrefabValue containing the target types and the prefab entity. Multiple values can be associated with the same key (hence multi-hashmap).

  • public void RemovePrefab(Entity prefab, TriggerData triggerData)
    Removes a specific PrefabValue for the given trigger key. Iterates all values for the key and removes the first matching entry where both target types and prefab entity match the passed data.

  • public bool HasAnyPrefabs(TriggerType triggerType, Entity triggerPrefab)
    Returns true if there is at least one PrefabValue stored for the given triggerType and triggerPrefab pair.

  • public bool TryGetFirstPrefab(TriggerType triggerType, TargetType targetType, Entity triggerPrefab, out Entity prefab, out Iterator iterator)
    Attempts to find the first prefab entity for the given trigger key that matches the supplied targetType filter. If targetType == TargetType.Nothing then any target type is accepted. On success returns true, sets prefab to the found entity and returns an Iterator (iterator.m_Iterator) suitable for continuing enumeration via TryGetNextPrefab.

  • public bool TryGetNextPrefab(TriggerType triggerType, TargetType targetType, Entity triggerPrefab, out Entity prefab, ref Iterator iterator)
    Continues enumeration started by TryGetFirstPrefab using the provided iterator. Scans subsequent values for the same key and returns the next prefab matching the targetType filter. Returns true and sets prefab on success; otherwise returns false and sets prefab to Entity.Null.

Notes: - TargetType is used as a bitmask; matching is performed via bitwise AND and compared against TargetType.Nothing. - The TryGetFirst/Next pattern relies on the Iterator struct which wraps NativeParallelMultiHashMapIterator; the iterator must be passed by ref to TryGetNextPrefab.

Usage Example

// Create (persistent for long-lived storage)
var triggerPrefabs = new TriggerPrefabData(Allocator.Persistent);

// Example: add a prefab for a trigger (triggerData is assumed available)
triggerPrefabs.AddPrefab(prefabEntity, triggerData);

// Query: iterate all prefabs for a trigger that match a target type
if (triggerPrefabs.TryGetFirstPrefab(triggerType, desiredTargetType, triggerPrefabEntity, out var foundPrefab, out var iter))
{
    // use foundPrefab
    do
    {
        // process foundPrefab
    }
    while (triggerPrefabs.TryGetNextPrefab(triggerType, desiredTargetType, triggerPrefabEntity, out foundPrefab, ref iter));
}

// Cleanup when done
triggerPrefabs.Dispose();