Playing your own clip
This page uses Vector3, AudioClip from Unity. The glossary says what each one is.
An AudioClip is Unity’s name for a sound held in memory. These calls play one, and Find checks that a mixer group exists. A mixer group is one of the game’s volume channels, so the player’s volume sliders apply to your sound.
Sounds plays your own clips
Use Find to get one of the game’s volume channels, such as Effects, then PlayClip or PlayClipAt to play your clip through it.
public static class Sounds
{
public static AudioMixerGroup Find(string name);
public static bool PlayClip(AudioClip clip, string group, float volume = 1f);
public static bool PlayClipAt(AudioClip clip, string group, Vector3 position, float volume = 1f);
}
| Method | Returns | What it does |
|---|---|---|
Find(name) | AudioMixerGroup | The game’s volume channel with this name. null when there is none. |
PlayClip(clip, group, volume) | bool | Plays a clip with no position. Returns false when it could not start. |
PlayClipAt(clip, group, position, volume) | bool | Plays a clip at a place in the world. Returns false when it could not start. |
Things to know
Find(name)looks up one of the game’s volume channels, and gives younullwhen there is no channel with that name.Effectsis the channel the game uses for ordinary sound effects. The game also has channels namedMusic,Menu,Ambience,Footsteps,Underwaterand others. (It searches the game’s mainAudioMixerwithFindMatchingGroups.Effectsis the default group ofMiscAudioPlayer.)- Call
Findfirst if you want to be sure a channel exists before you rely on it. PlayClipplays the clip once with no position, andPlayClipAtplays it once at a place in the world. The player’s volume sliders apply, because the sound goes through the group you name.volumegoes from 0 (silent) to 1 (full). The game removes the temporary sound source for you shortly after the clip ends. (The clip plays on a temporary object that is destroyed afterclip.length * 1.1seconds.)
Limits
- Both calls return
falsewhen the group is missing or the clip can’t be played. Wrong arguments throw an error instead: a clip that isnull, a group that isnullor empty, or a volume below 0 or above 1.