Audio Extensions
Play Clip
This plays the specified AudioClip on the AudioSource.
AudioSource audioSource = GetComponent<AudioSource>();
audioSource.PlayClip(audioClip);
Set Volume
This sets the volume of the AudioSource to 0.5 (50% of maximum volume).
AudioSource audioSource = GetComponent<AudioSource>();
audioSource.SetVolume(0.5f);
Get Volume
This gets the volume of the AudioSource with a default value of 0.0f.
AudioSource audioSource = GetComponent<AudioSource>();
audioSource.GetVolume();
Set Pitch
This sets the pitch of the AudioSource to 0.5.
AudioSource audioSource = GetComponent<AudioSource>();
audioSource.SetPitch(0.5f);
Set 3D Properties
This sets the 3D properties of the AudioSource with a 1f value of minDistance, 500f of maxDistance, and 1f of spatialBlend.
AudioSource audioSource = GetComponent<AudioSource>();
audioSource.Set3DProperties(1f, 500f, 1f);
Pause Clip
This pauses the currently playing audio clip.
AudioSource audioSource = GetComponent<AudioSource>();
audioSource.PauseClip();
Resume Clip
This resumes playback of the audio clip from where it was paused.
AudioSource audioSource = GetComponent<AudioSource>();
audioSource.ResumeClip();
Stop Clip
This stops the currently playing audio clip.
AudioSource audioSource = GetComponent<AudioSource>();
audioSource.StopClip();
Fade Out
This starts fading out the audio over a duration of 2 seconds, gradually decreasing the volume to 0 and stopping the playback.
AudioSource audioSource = GetComponent<AudioSource>();
StartCoroutine(audioSource.FadeOut(2f));
Fade In
This starts fading in the audio over a duration of 2 seconds, gradually increasing the volume to 1.
AudioSource audioSource = GetComponent<AudioSource>();
StartCoroutine(audioSource.FadeIn(2f));
Crossfade
This crossfades the current audio clip with the given audio clip over a duration of 2 seconds, gradually decreasing and increasing the volume from 0 to 1.
AudioSource audioSource = GetComponent<AudioSource>();
StartCoroutine(audioSource.Crossfade(audioClip, 2f));
Last updated