Time for action – Adding ambient sounds

While we're in our town we want it to sound like a town that is in the middle of the forest. To accomplish this we should have some ambient sound that is low volume that just blends in to the environment. A good source of Creative Commons licensed sounds is The Freesound Project which can be found at http://www.freesound.org. There are a number of good candidates on the site and I have picked up one from user reinsamba for this test. You will find a copy of it in the assets folder of the project called evening in the forest.wav.

  1. To keep your assets organized create an Ambient Audio folder in your project.
  2. Simply drag the asset, an evening in the forest.wav file in this case, into the project and Unity will perform the necessary conversion:
    Time for action – Adding ambient sounds

    Now you have a usable AudioSource that can be added to other GameObjects in the scene. But remember, Unity will not distort the assets unless we tell it to by changing some properties of the original audio file. We brought in a fairly sizable AudioSource as a .wav file.

  3. Tell Unity to compress this asset by selecting Compressed from the Audio Format option so that the file doesn't take up as much space. Also, check the Hardware decoding checkbox so that Unity will use the iOS hardware built into an iOS device:
    Time for action – Adding ambient sounds

    If hardware isn't available, Unity will automatically fall back to software decoding. In our case here we are going from a 26.2MB Wav file to a 0.6MB compressed MP3 file. If you want a higher quality sample you can adjust the Compression slider to a higher value, but in our case this is simple ambient noise so we don't need an excessive amount of fidelity.

    Now that we have created the AudioSource we need to attach it to a GameObject so that it will play. Since this is the sound of the environment, we can attach this to the ground that the player will walk around on.

  4. Simply drag the Audio source onto the ground in the Scene view, or drag it over to the ground in the Hierarchy view. Either will result in an AudioSource Component being added to the ground.
  5. Ensure that the audio continues to play by selecting the Loop option from the Audio Source properties in the Inspector:
    Time for action – Adding ambient sounds

We have told this AudioSource to loop its audio so we should hear this audio clip for the duration of the game while we're on this ground GameObject. One special note about compressed audio in iOS is that the compression process may tweak the beginning or end of your audio data, so you will want to listen to it after compressing it to make sure that it still loops properly.

We didn't necessarily have to attach the sound to the ground, we could have attached it to a different object. For example, if we wanted to give a marketplace the sounds of marketplace chatter we could attach that directly to the GameObject that represents the marketplace and when the player walks away from it, the sound would diminish.

What just happened?

We just created an ambient sound track for our world. By importing a .wav file and compressing it we have integrated an asset that is suitable for use on our mobile platform and can use the iOS device hardware to decompress the audio stream. Since we've attached this AudioSource to the ground of our world, we can be certain that it will be audible as we move through our environment. With this we have created some atmosphere for our world and given this area of the world a personality.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.172.180