Game.Prefabs.TrafficAccident
Assembly: Assembly-CSharp
Namespace: Game.Prefabs
Type: class
Base: ComponentBase
Summary:
TrafficAccident is a prefab component used to configure a traffic accident event for the game's event system. It exposes editable fields (site type, subject type, accident type and occurrence probability) which are written into an ECS component (TrafficAccidentData) when the prefab is initialized. The class also contributes required ECS archetype components (Game.Events.TrafficAccident and TargetElement) so the event system can create and manage accidents at runtime.
Fields
-
public EventTargetType m_RandomSiteType
Defines the type of site chosen randomly for the accident (default: EventTargetType.Road). This controls where the event may spawn. -
public EventTargetType m_SubjectType
Defines the subject type involved in the accident (default: EventTargetType.MovingCar). This controls what entity types the event targets. -
public TrafficAccidentType m_AccidentType
Specifies the subtype of traffic accident (e.g., minor collision, major crash). Used by the event logic to determine behavior/visuals of the accident. -
public float m_OccurrenceProbability
Probability that the event will occur when evaluated (default: 0.01). Note: when this value is written into the ECS component it is assigned to a field named m_OccurenceProbability (spelling mismatch) — be aware of the typo in the component field name.
Properties
- None (this class exposes configuration via public fields and overrides; it doesn't declare C# properties).
Constructors
public TrafficAccident()
Default public constructor. The class relies on Unity/engine lifecycle methods and the ComponentBase base class for initialization; no custom initialization logic is performed in a constructor.
Methods
public override void GetPrefabComponents(HashSet<ComponentType> components)
Adds the component type required for prefab-data storage to the provided set. Implementation adds:-
ComponentType.ReadWrite
() This ensures the prefab will include the data component used to transfer the configured fields into the ECS world. -
public override void GetArchetypeComponents(HashSet<ComponentType> components)
Adds run-time archetype components required when creating entities for this event. Implementation adds: - ComponentType.ReadWrite
() -
ComponentType.ReadWrite
() These components prepare the ECS entity to be managed by the traffic-accident event systems. -
public override void Initialize(EntityManager entityManager, Entity entity)
Called during prefab instantiation to write the configured prefab fields into the entity's TrafficAccidentData component. The method: - Creates a TrafficAccidentData struct
- Copies m_RandomSiteType, m_SubjectType, m_AccidentType and m_OccurrenceProbability into the struct
- Calls entityManager.SetComponentData(entity, componentData) Note: the code assigns m_OccurrenceProbability to componentData.m_OccurenceProbability (typo in target field name). Verify matching struct field names when reading/writing.
Additional notes: - The class is decorated with [ComponentMenu("Events/", new Type[] { typeof(EventPrefab) })], making it available in the editor menu under Events as an EventPrefab-derived component. - Uses Unity.Entities API (EntityManager, Entity, ComponentType) and relies on the TrafficAccidentData and Game.Events.TrafficAccident structs/types being defined elsewhere in the codebase.
Usage Example
// Example: creating/configuring a TrafficAccident prefab in code (conceptual)
var prefab = gameObject.AddComponent<Game.Prefabs.TrafficAccident>();
prefab.m_RandomSiteType = EventTargetType.Road;
prefab.m_SubjectType = EventTargetType.MovingCar;
prefab.m_AccidentType = TrafficAccidentType.MinorCollision;
prefab.m_OccurrenceProbability = 0.02f;
// When the prefab is converted/initialized, its Initialize override will copy these values
// into the entity's TrafficAccidentData so the ECS event systems can use them.