Class SoundComponent
Base class for vehicle sound components. Creates and owns one AudioSource on the container GameObject, routes it through the mixer group, and applies the SoundManager master volume.
Inheritance
Inherited Members
Namespace: NWH.VehiclePhysics2.Sound.SoundComponents
Assembly: NWH.VehiclePhysics2.dll
Syntax
[Serializable]
public abstract class SoundComponent : VehicleComponent
Remarks
Derived classes supply the container, mixer group and priority, and override the Init* properties for loop, clip, volume, spatial blend and doppler. Set volume through SetVolume, not source.volume, or the master volume is skipped.
Fields
baseVolume
Base volume level for this sound component before master volume is applied.
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.
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 |
HasPlayableClip
Declaration
protected bool HasPlayableClip { get; }
Property Value
| Type | Description |
|---|---|
| bool |
InitClip
Gets the initial audio clip to assign to the AudioSource during initialization.
Declaration
public virtual AudioClip InitClip { get; }
Property Value
| Type | Description |
|---|---|
| AudioClip |
InitDopplerLevel
Gets the initial doppler level for the AudioSource.
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.
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.
Declaration
public virtual float InitSpatialBlend { get; }
Property Value
| Type | Description |
|---|---|
| float |
InitVolume
Gets the initial volume for the AudioSource.
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 a clip from the default resources Sound folder and assigns it as the first clip.
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)
Adds an AudioSource to the container and applies the Init* properties to it.
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.
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 AudioSource volume with the master volume applied. Use this rather than writing source.volume directly.
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()
One-time setup, once per vehicle startup and after the VehicleController reference is set. Sets state.initialized on completion; an override that fails must leave it false.
Declaration
protected override void VC_Initialize()