11 Playing audio: sound effects and music
This chapter covers
- Importing and playing audio clips for various sound effects
- Using 2D sounds for the UI and 3D sounds in the scene
- Modulating the volume of all sounds when they play
- Playing background music while the game is being played
- Fading in and out between different background tunes
Although graphics get most of the attention when it comes to content in video games, audio is crucial, too. Most games play background music and have sound effects. Accordingly, Unity has audio functionality so that you can put sound effects and music into your games. Unity can import and play a variety of audio file formats, adjust the volume of sounds, and even handle sounds playing from a specific position within the scene.
NOTE
Audio is handled the same way for both 2D and 3D games. Although the sample project in this chapter is a 3D game, everything we’ll do applies to 2D games as well.