Game.Prefabs.Effects.SFX
Assembly: Assembly-CSharp
Namespace: Game.Prefabs.Effects
Type: public class SFX : ComponentBase
Base: ComponentBase
Summary:
SFX is a prefab component used to configure and initialize short-lived or persistent audio effects (sound effects) for the game. It exposes inspector-configurable fields for clip, volume, pitch, spatialization and rolloff, and during LateInitialize it registers the SFX with the game's AudioManager and writes runtime AudioEffectData and an AudioSourceData buffer entry onto the entity. This component is intended to be attached to effect prefabs so that the ECS-side audio systems can play and manage the sound.
Fields
-
public AudioClip m_AudioClip
Reference to the Unity AudioClip used by this effect. -
public float m_Volume
Linear volume multiplier (0.0–1.0). Default = 1f. -
public float m_Pitch
Pitch multiplier for playback (-3.0–3.0). Default = 1f. -
public float m_SpatialBlend
Spatial blend between 2D and 3D audio (0.0–1.0). Default = 1f. -
public float m_Doppler
Doppler level (0.0–1.0). Default = 1f. -
public float m_Spread
Spread angle (degrees) used for 3D sound spread. -
public AudioRolloffMode m_RolloffMode
Rolloff mode (Logarithmic, Linear, or Custom). Default = AudioRolloffMode.Linear. -
public float2 m_MinMaxDistance
Min and Max distances for attenuation (min = x, max = y). Default = (1f, 200f). m_MinMaxDistance.y is used to set m_MaxDistance on AudioEffectData. -
public bool m_Loop
Whether the audio should loop. -
public MixerGroup m_MixerGroup
Mixer group (game-specific mixer grouping) used for routing this SFX. -
public byte m_Priority
Audio priority (0..255). Default = 128. -
public AnimationCurve m_RolloffCurve
Custom rolloff curve when using custom rolloff mode. -
public float3 m_SourceSize
Source size used by the audio system for spatialization. -
public float2 m_FadeTimes
Fade-in and fade-out times used by the audio system. -
public bool m_RandomStartTime
If true, playback will choose a random start time in the clip.
Properties
- This class does not declare C# properties; it exposes public fields for configuration and overrides component lifecycle methods to populate ECS components.
Constructors
public SFX()
No explicit constructor is defined; the class relies on the default parameterless constructor provided by C#. All configuration is expected to be set in the inspector or prefab data.
Methods
-
public override void GetPrefabComponents(HashSet<ComponentType> components)
Adds required component types to the provided HashSet for the prefab archetype. This SFX registers that it needs AudioEffectData and AudioSourceData component types (both ReadWrite) on the entity so that the audio systems have the required components at runtime. -
public override void LateInitialize(EntityManager entityManager, Entity entity)
Called during prefab entity initialization. Behavior: - Calls base.LateInitialize.
- Obtains the managed AudioManager system from the entityManager.World and registers this SFX (using AudioManager.RegisterSFX), receiving an audio clip id.
- Writes an AudioEffectData component to the entity containing m_AudioClipId, m_MaxDistance from m_MinMaxDistance.y, m_SourceSize and m_FadeTimes.
-
Ensures the entity has an AudioSourceData buffer with one entry and populates the first element with m_SFXEntity = entity. This method bridges the inspector-prefab values into ECS data used by the audio playback systems.
-
public override void GetArchetypeComponents(HashSet<ComponentType> components)
Empty override. The component does not add any archetype components here; GetPrefabComponents is used instead to add runtime component requirements.
Usage Example
// This illustrates the lifecycle override used by SFX; actual SFX prefab usage
// is normally performed via the editor/prefab system rather than manually.
[Preserve]
public override void LateInitialize(EntityManager entityManager, Entity entity)
{
base.LateInitialize(entityManager, entity);
// The SFX component's LateInitialize registers the clip with AudioManager
// and writes AudioEffectData and AudioSourceData to the entity.
// No additional code is required for the basic registration performed in SFX.cs.
}
Additional notes: - SFX is intended to be configured in the prefab inspector (assign the AudioClip and tuning fields). The ECS data written in LateInitialize (AudioEffectData + AudioSourceData) is what the runtime audio systems consume to play and manage the sound effect. - If you need custom behavior (e.g., different buffer sizing, additional audio data), extend or replace the prefab component and ensure the corresponding ECS components are produced in GetPrefabComponents / LateInitialize.