Class SoundComponent
Abstract base class for all vehicle sound components, providing standardized audio management with support for multiple audio sources, clips, and advanced audio processing.
Inheritance
Inherited Members
Namespace: NWH.VehiclePhysics2.Sound.SoundComponents
Assembly: NWH.VehiclePhysics2.dll
Syntax
[Serializable]
public abstract class SoundComponent : VehicleComponent
Remarks
SoundComponent creates a unified interface above Unity's AudioSource system, ensuring consistent volume control, master volume integration, and proper audio state management. Each component can manage multiple AudioSources and AudioClips for complex sound behaviors and variation.
Key features include: - Multi-clip support with random selection for audio variation - Automatic AudioSource management and creation - Master volume integration and per-component volume control - 3D spatial audio positioning with configurable falloff - Audio Mixer integration for advanced audio processing - LOD-based audio management for performance optimization - Fade in/out capabilities for smooth audio transitions
Sound components automatically handle AudioSource lifecycle, clip assignment, and spatial audio positioning. They integrate with the vehicle's component state system for proper enable/disable behavior and LOD management to maintain performance across multiple vehicles.
Fields
baseVolume
Base volume level for this sound component before master volume is applied. This value is multiplied by the master volume to get the final AudioSource volume.
Declaration
[FormerlySerializedAs("volume")]
[Range(0, 1)]
[Tooltip("Base volume of the sound component.")]
public float baseVolume
Field Value
| Type | Description |
|---|---|
| float |
clips
List of audio clips this component can use. Components that support multiple clips will choose randomly from this list for variation. Single-clip components will only use the first clip in the list. Refer to the component-specific documentation for clip usage behavior.
Declaration
[Tooltip("List of audio clips this component can use. Some components can use multiple clips in which case they will be chosen at random, and some components can use only one in which case only the first clip will be selected. Check manual for more details.")]
public List<AudioClip> clips
Field Value
| Type | Description |
|---|---|
| List<AudioClip> |
source
The Unity AudioSource component used by this sound component. Created automatically during initialization.
Declaration
[NonSerialized]
[Tooltip("Audio source for this component.")]
public AudioSource source
Field Value
| Type | Description |
|---|---|
| AudioSource |
Properties
AudioMixerGroup
Gets the Audio Mixer Group this component's AudioSource should output to. Used to route audio through the vehicle's audio mixer for processing and mixing.
Declaration
public abstract AudioMixerGroup AudioMixerGroup { get; }
Property Value
| Type | Description |
|---|---|
| AudioMixerGroup |
Clip
Gets or sets the first clip in the clip list.
Declaration
public AudioClip Clip { get; set; }
Property Value
| Type | Description |
|---|---|
| AudioClip |
ContainerGO
Gets the GameObject container where the AudioSource should be created. Used to position audio sources at specific locations on the vehicle.
Declaration
public abstract GameObject ContainerGO { get; }
Property Value
| Type | Description |
|---|---|
| GameObject |
InitClip
Gets the initial audio clip to assign to the AudioSource during initialization. Override in derived classes to provide a default clip.
Declaration
public virtual AudioClip InitClip { get; }
Property Value
| Type | Description |
|---|---|
| AudioClip |
InitDopplerLevel
Gets the initial doppler level for the AudioSource. Override in derived classes to use a different doppler level than the manager default.
Declaration
public virtual float InitDopplerLevel { get; }
Property Value
| Type | Description |
|---|---|
| float |
InitLoop
Gets whether the AudioSource should loop continuously. Override in derived classes to enable looping for continuous sounds like engine running.
Declaration
public virtual bool InitLoop { get; }
Property Value
| Type | Description |
|---|---|
| bool |
InitPlayOnAwake
Gets whether the AudioSource should automatically play on awake. Override in derived classes to enable automatic playback on initialization.
Declaration
public virtual bool InitPlayOnAwake { get; }
Property Value
| Type | Description |
|---|---|
| bool |
InitSpatialBlend
Gets the initial spatial blend value between 2D (0) and 3D (1) audio. Override in derived classes to use a different spatial blend than the manager default.
Declaration
public virtual float InitSpatialBlend { get; }
Property Value
| Type | Description |
|---|---|
| float |
InitVolume
Gets the initial volume for the AudioSource. Override in derived classes to set a different starting volume.
Declaration
public virtual float InitVolume { get; }
Property Value
| Type | Description |
|---|---|
| float |
InitializeWithNoClips
Gets whether this component can be initialized without any audio clips assigned. Most components require at least one clip to function.
Declaration
public virtual bool InitializeWithNoClips { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Priority
Gets the priority level for the AudioSource. Lower values indicate higher priority. Range is 0 (highest) to 256 (lowest). Used by Unity's audio system when the maximum number of audio sources is reached.
Declaration
public abstract int Priority { get; }
Property Value
| Type | Description |
|---|---|
| int |
RandomClip
Gets a random clip from the clips list.
Declaration
public AudioClip RandomClip { get; }
Property Value
| Type | Description |
|---|---|
| AudioClip |
Methods
AddDefaultClip(string)
Loads and assigns a default audio clip from the Resources folder. Used during initialization to provide default sounds when no clip is assigned.
Declaration
public virtual void AddDefaultClip(string clipName)
Parameters
| Type | Name | Description |
|---|---|---|
| string | clipName | Name of the audio clip file in the default resources Sound folder. |
CreateAndRegisterAudioSource(AudioMixerGroup, GameObject)
Creates and configures an AudioSource component with the specified mixer group and container. Sets up all initial audio properties including volume, pitch, spatial blend, and mixer routing.
Declaration
protected AudioSource CreateAndRegisterAudioSource(AudioMixerGroup mixerGroup, GameObject container)
Parameters
| Type | Name | Description |
|---|---|---|
| AudioMixerGroup | mixerGroup | The Audio Mixer Group to route the audio through. |
| GameObject | container | The GameObject to attach the AudioSource to. |
Returns
| Type | Description |
|---|---|
| AudioSource | The created and configured AudioSource, or null if creation failed. |
Play()
Plays the currently assigned audio clip. Does nothing if the source is disabled or already playing.
Declaration
public virtual void Play()
Play(int)
Plays the audio clip at the specified index from the clips list.
Declaration
public virtual void Play(int clipIndex)
Parameters
| Type | Name | Description |
|---|---|---|
| int | clipIndex | Index of the clip to play. |
PlayForDurationCoroutine(int, float)
Coroutine that plays a specific clip for a fixed duration then stops.
Declaration
public IEnumerator PlayForDurationCoroutine(int clipIndex, float duration)
Parameters
| Type | Name | Description |
|---|---|---|
| int | clipIndex | Index of the clip to play from the clips list. |
| float | duration | Duration in seconds to play the clip before stopping. |
Returns
| Type | Description |
|---|---|
| IEnumerator | Coroutine enumerator. |
PlayRandomClip()
Plays a randomly selected clip from the clips list. Useful for adding variation to repetitive sounds.
Declaration
public virtual void PlayRandomClip()
SetPitch(float)
Sets the pitch of the AudioSource. Pitch affects playback speed and perceived frequency. Values are clamped between 0 and 5.
Declaration
public virtual void SetPitch(float pitch)
Parameters
| Type | Name | Description |
|---|---|---|
| float | pitch | Pitch value where 1 is normal speed, 2 is double speed, 0.5 is half speed. |
SetVolume(float)
Sets the volume of the AudioSource with master volume applied. Always use this method instead of directly modifying source.volume to ensure the master volume multiplier is properly applied.
Declaration
public virtual void SetVolume(float volume)
Parameters
| Type | Name | Description |
|---|---|---|
| float | volume | Base volume level before master volume multiplication. |
Stop()
Stops playback of the AudioSource if it is currently playing.
Declaration
public virtual void Stop()
VC_Disable(bool)
Stops and disables the AudioSource belonging to this SoundComponent.
Declaration
public override bool VC_Disable(bool calledByParent)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | calledByParent |
Returns
| Type | Description |
|---|---|
| bool |
Overrides
VC_Enable(bool)
Enables the AudioSource belonging to this SoundComponent.
Declaration
public override bool VC_Enable(bool calledByParent)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | calledByParent |
Returns
| Type | Description |
|---|---|
| bool |
Overrides
VC_Initialize()
Initializes the component's internal systems and resources. Called once during vehicle startup after the VehicleController reference is set.
Declaration
protected override void VC_Initialize()
Overrides
Remarks
Override this to set up component-specific resources, cache references, or perform one-time initialization. Sets state.initialized to true on completion.