Game.Prefabs.StatisticTriggerPrefab
Assembly: Assembly-CSharp (game code)
Namespace: Game.Prefabs
Type: class
Base: PrefabBase
Summary:
Prefab used to configure a statistics-based trigger in the game. When the prefab is converted to ECS this class supplies TriggerData and StatisticTriggerData on the resulting entity. It exposes connections to StatisticsPrefab instances (the statistic to sample and an optional normalizer), parameters like time frame and minimum samples, and ensures required dependencies and component types are registered during prefab initialization. It also applies a minimum-sample constraint for daily statistics collections.
Fields
-
public StatisticTriggerType m_Type
Specifies the kind of statistic trigger (enum). Controls how the statistic value is interpreted or compared when the trigger fires. -
public StatisticsPrefab m_StatisticPrefab
Reference to the StatisticsPrefab that provides the statistic to evaluate. If set, this prefab is added to dependencies and resolved to an entity during LateInitialize. -
public int m_StatisticParameter
Index/ID of the statistic parameter within the referenced StatisticsPrefab to use. -
public StatisticsPrefab m_NormalizeWithPrefab
Optional reference to another StatisticsPrefab used for normalization (e.g., per-capita). If set, this prefab is added to dependencies and resolved to an entity during LateInitialize. -
public int m_NormalizeWithParameter
Index/ID of the parameter on the normalization prefab to use. -
public int m_TimeFrame = 1
Time frame (number of samples/period units) over which the statistic is evaluated. -
public int m_MinSamples = 1
Minimum number of samples required before the trigger can consider the statistic valid. This value may be increased automatically for certain collection types (see LateInitialize behavior).
Properties
- This class does not declare properties of its own; it exposes public fields for inspector configuration.
Constructors
public StatisticTriggerPrefab()
Default constructor (uses base PrefabBase constructor). Instances are typically created/edited in Unity as prefab assets rather than constructed at runtime in code.
Methods
-
public override void GetDependencies(List<PrefabBase> prefabs)
Collects other prefabs that must be loaded/initialized before this prefab. Adds m_StatisticPrefab and m_NormalizeWithPrefab (if set) to the provided list so their entities are created/resolved first. -
public override void GetPrefabComponents(HashSet<ComponentType> components)
Registers the ECS component types that will be present on the entity created from this prefab. Adds TriggerData and StatisticTriggerData (ReadWrite) to the components set. -
public override void LateInitialize(EntityManager entityManager, Entity entity)
Converts the prefab configuration into runtime ECS component data: - Obtains a managed PrefabSystem to resolve referenced StatisticsPrefab instances into Entities.
- Constructs a StatisticTriggerData instance and sets:
- m_Type from m_Type field
- m_StatisticEntity from the resolved m_StatisticPrefab (if provided)
- m_StatisticParameter from m_StatisticParameter
- m_NormalizeWithPrefab as resolved entity (if provided)
- m_NormalizeWithParameter from m_NormalizeWithParameter
- m_TimeFrame and m_MinSamples from corresponding fields
- If either referenced statistics prefab collects data with StatisticCollectionType.Daily, enforces a larger minimum sample count: m_MinSamples = max(m_MinSamples, 32 + max(0, m_TimeFrame - 1)) This ensures enough daily samples are present before triggering.
- Writes StatisticTriggerData to the entity and appends a TriggerData buffer element to the entity with:
- m_TriggerType = TriggerType.StatisticsValue
- m_TargetTypes = TargetType.Nothing
- m_TriggerPrefab = entity This sets up the entity as a statistics-value trigger that other systems can process.
Usage Example
// Example: retrieving the configured StatisticTriggerData at runtime
var prefabSystem = entityManager.World.GetOrCreateSystemManaged<PrefabSystem>();
Entity triggerEntity = prefabSystem.GetEntity(myStatisticTriggerPrefab); // resolve prefab to entity
if (entityManager.HasComponent<StatisticTriggerData>(triggerEntity))
{
var statTrigger = entityManager.GetComponentData<StatisticTriggerData>(triggerEntity);
// statTrigger.m_Type, m_StatisticEntity, m_TimeFrame, m_MinSamples etc. are available
}
This example shows how the prefab's configured fields become a StatisticTriggerData component on the resulting entity and how you can read that component in systems.