So in Part 1 of Music and Sound I talked about creating your own game music. Now I’ll show you how to integrate the music into Unity.
Unity updated many things about their engine in the latest release. One of the biggest overhauls they did was toward sound creation and manipulation within the engine. This post will go over the basics of using the sound engine but if you want to take it to the next level here is a link to Unity’s advanced tutorial on making sound groups and mixers: http://blogs.unity3d.com/2014/07/24/mixing-sweet-beats-in-unity-5-0/.
Background Music
Sound and Music is sometimes the most underrated thing game developers take into consideration when making their games. But what they don’t realize is that extra addition can take a game from great, to epic. Imagine Super Mario or any of the Final Fantasy Games without their phenomenal score. The ability to completely immerse the player would be lost. Movies use music to manipulate the mood of the audience in order to provoke certain emotions; fear, sadness, anxiety. Games can use music to do the same. For example, speeding up music can create a feeling of intensity and urgency.
Implementing Sounds in Unity
Before we begin here are some Unity terms that you should know:
- Audio Listener – controls audio output to the headphones.
- Audio Source – where the sound comes from
- Audio Clip – sound file
- Audio Mixer – Controls Audio Groups
- Audio Group – Channel for certain clips and effects
To create sound in Unity you need at least three things. I’ll leave Audio Mixer and Audio Groups out for now.
- Audio Clip
- Audio Source
- Audio Listener
Step 1: Create Audio Game Objects
In your hierarchy create a empty gameobject and call it SoundManager. I like to organize my sounds by putting them in an empty SoundManager Object.
Step 2: Add Audio Source to the SoundManager
Step 3: Add Audio Clip to source
Create an Audio folder in your “Assets” folder. This folder will hold all your sound clips.
Take the sound file you created from the previous post, and with your Explorer (or Finder for Macs) move it to this Audio folder.
Then add this clip to the Audio Clip area of the Audio Source you added to SoundManager.
Make sure that the Loop and Play on Awake boxes are selected as well. This will ensure that your music will play throughout your game and will play at the creation of the SoundManger object.
Step 4: Add SoundManager Script
In your scripts folder create a new script and call it SoundManager. This script can control certain effects that can be applied to your clips, like pitch controls; however I’m only use it to will ensure the game music is playing.
public AudioSource musicSource;
public static SoundManager instanceSM = null;
// Use this for initialization
void Awake () {
}
// Update is called once per frame
void Update () {
if (!(GetComponent<AudioSource>().isPlaying))
{
GetComponent<AudioSource>().Play();
}
else
{
//Debug.log("Something is wrong with Music.");
}
}
And there you have it. If you press play your music should start playing. I’ll continue talking about adding sound effects to specific game objects in my next post, as well as getting into Audio Mixers.
Happy Coding
-TheNappingKat