Unity Cookbook. Audio playback

Audio playback

How to play a sound

  • You need two components to play a sound
  • AudioSource
    • You can have multiple of these
  • AudioListener
    • There can only be one

AudioSource

  • You can have multiple of these
  • Manual: AudioSource component
  • Insert to every GameObject that should be able to make a sound
    • E.g., if a Coin GameObject should have a *bling* sound effect when collected
    • Drag and drop a bling.wav sound effect file into the component
    • Supported audio file formats are listed here: Manual: Audio Files

Controlling audio with code

  • Script Reference: AudioSource class
  • The most important functions to control the component are:
    • .Play()
    • .Pause()
    • .Stop()
      [SerializeField] AudioSource audio;
      if (PlayerGrabbedCoin)
        audio.Play();
      if (DoSomethingElse)
        audio.Stop();
      

      AudioSource component

  • For background music, tick Loop checkbox to play it indefinitely

AudioListener

Spatial Audio

  • For sound effects that get louder when the AudioListener is closer to the source
  • AudioSource settings
    • Set spatial blend to 1 (3D)
    • In 3D Sound Settings, set Volume Rolloff to Logarithmic Rolloff
      • (You can customize the graph yourself!)
    • Set Max distance to something fitting for your scene
      • This is visualized as a sphere

Music Exercise

Download royalty-free music by Kevin MacLeod from here.

Play it on a loop in a scene.

SFX Exercise

Time to add sound effects to your game! Either:

a) Download from freesound (you’ll need an account) b) Record with your PC mic by using Audacity c) Create new sounds by using bfxr

Then, add sounds to your game that can be triggered to play a) when a GameObject gets created b) Calling audioSource.Play() with code

2) looping sound that plays all the time

Note: Playing sound when something gets destroyed

  • If you add an audio source to a GameObject that gets destroyed when the sound should be played, the sound stops abruptly
  • Instead, add the audio source to some GameObject that you know won’t be destroyed
  • E.g, for playing a sound when collecting a coin, add a CoinPlayer GameObject with an Audiosource component, and call its .Play() method when collecting the coin.

Fading between volume levels

There are many ways to fade in/out audio. Here, we present an example that needs very minimal code.

  • Create a new audio mixer asset: Create > Audio Mixer.
  • Name it “FadeMixer” or something like that.
    • Click Open

width:500px

  • In the Mixer view:
    • Name the existing Snapshot to VolumeOn
    • Create a new snapshot VolumeOff
      • Set its volume in the Master mixer group’s slider to -80dB.
    • Right click VolumeOff and select Set as start Snapshot.

  • In project view, expand the mixer asset to see the Master mixer group and the two snapshots VolumeOn and VolumeOff.
  • Drag the Master group to the audio source component that you want to fade.
  • Then, in the script where you want to control the fade, create a new variable:
    using UnityEngine.Audio;
    ...
    [SerializeField] AudioMixerSnapshot VolumeOn;
    
    • Drag the VolumeOn snapshot to the inspector to the corresponding variable.
    • Then, we can fade from the default VolumeOff snapshot to the VolumeOn snapshot by calling the method
      VolumeOn.TransitionTo(3); // fade takes 3 seconds
      

Here’s what the audio mixer asset looks like when expanded.

Here, the Master audio mixer group is dragged to Audio Source, and the VolumeOn snapshot to SceneManager.