Skip to content

Game.Prefabs.Effects.AudioRandomize

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

Type: class

Base: ComponentBase

Summary:
AudioRandomize is a component prefab used to configure a set of sound-effect prefabs (EffectPrefab) from which runtime code/systems can pick one at random. At prefab initialization it validates the referenced prefabs for the presence of SFX components (and warns if any are missing or looping), registers the prefab component type AudioRandomizeData, and fills a DynamicBuffer on the prefab entity with entities referencing the configured SFX prefabs. This component is intended to be attached to effect prefabs to provide randomized audio selection at runtime.


Fields

  • public EffectPrefab[] m_SFXes
    Array of EffectPrefab references exposed in the editor (editor name "Sound Effects"). These are the sound effect prefabs that will be validated and converted to entries in the AudioRandomizeData buffer during LateInitialize. If null or empty a warning is logged.

Properties

  • None (no public properties declared)

Constructors

  • public AudioRandomize()
    Default constructor (implicit). No special construction logic in the class itself; initialization is handled in LateInitialize.

Methods

  • public override void GetArchetypeComponents(HashSet<ComponentType> components)
    This override is intentionally empty — AudioRandomize does not add any archetype-time components.

  • public override void GetPrefabComponents(HashSet<ComponentType> components)
    Adds ComponentType.ReadWrite() to the provided set. This informs the prefab system that entities for this prefab will have a DynamicBuffer.

  • public override void GetDependencies(List<PrefabBase> prefabs)
    Adds all EffectPrefab references from m_SFXes to the given prefab dependency list. This ensures referenced SFX prefabs are loaded/available when this prefab is processed.

  • public override void LateInitialize(EntityManager entityManager, Entity entity)
    Performs runtime validation and buffer population:

  • Obtains the PrefabSystem from the entity's World.
  • If m_SFXes is null, logs a warning ("has no sound effects").
  • For each referenced EffectPrefab:
    • Scans its components; warns if no SFX component is present on that prefab.
    • If an SFX component is present and its m_Loop is true, logs a warning (looping SFX may be undesirable for randomized one-shot audio).
  • Resizes the DynamicBuffer on the entity to match m_SFXes.Length and fills each element's m_SFXEntity with the entity returned by PrefabSystem.GetEntity(m_SFXes[k]).
  • This results in a buffer of entities referencing the concrete SFX prefabs to pick from at runtime.

Usage Example

// Example: runtime system or component picking a random SFX from the buffer created by AudioRandomize
DynamicBuffer<AudioRandomizeData> buffer = entityManager.GetBuffer<AudioRandomizeData>(entity);
if (buffer.Length == 0) return;

int index = UnityEngine.Random.Range(0, buffer.Length);
Entity sfxEntity = buffer[index].m_SFXEntity;

// Use the game's prefab/prefab-system API to instantiate/play the selected sfxEntity.
// The exact call depends on available PrefabSystem methods in the modding API, e.g.:
// PrefabSystem prefabSystem = entityManager.World.GetOrCreateSystemManaged<PrefabSystem>();
// prefabSystem.Spawn(sfxEntity, position, rotation); // illustrative; check actual API

Notes and tips for modders: - Assign EffectPrefab entries to m_SFXes in the prefab inspector; LateInitialize will convert them to runtime entity references. - The component logs warnings when a referenced EffectPrefab lacks an SFX component or when the SFX component is set to loop — review these warnings to avoid unintended audio behavior. - The component relies on AudioRandomizeData (a DynamicBuffer element type). Ensure systems that consume this buffer handle empty buffers safely. - Because GetArchetypeComponents is empty, this component does not modify ECS archetypes at prefab creation time beyond the explicit GetPrefabComponents registration.