Why Audio Can Make or Break Your Game

Một phần của tài liệu Game Development for iOS with Unity3D 2012 (Trang 220 - 224)

8. Optimizing for File Size and Performance 197

8.5 Why Audio Can Make or Break Your Game

Due to the nature of compression, images made up of solid blocks of color com- press much better than images with lots of different colors in them. Antialiased edges rarely compress as well as solid edges and you may find that antialiased-edged transpar- ent images often need to be stored in 16- or 32-bit uncompressed formats to retain any level of quality. Work with your artist or designer to experiment early on with different styles and settings to reach the best result with the minimum impact on memory usage.

8.4 Scale and Why It’s Important

One important point to consider, regardless of the 3D program you choose, is your modeling scale. Getting the correct scale is vitally important in building a realistic phys- ics simulation. One way to imagine this is to picture a ball dropping from the sky. In the real world, a ball will fall in the way you expect a ball to fall—at the rate of real-world gravity. Now make the ball one hundred times bigger and try to play tennis with giant tennis rackets. The way the ball moves and the way that the rackets swing are no longer what you would see at Wimbledon, and the effect might resemble playing tennis on the moon. Now scale everything back down and apply exactly the same physics to those smaller objects and they’ll move as they should.

8.5 Why Audio Can Make or Break Your Game

The soundtrack to your game can make or break it. Its game play is, of course, vitally important, but the overall experience is what people are going to be talking about after Figure 8.3. TexturePacker is software to take multiple images and convert them into a single atlas image.

the “game over” screen. There are many great games that have been let down by a lack of consistency between their elements. As an obvious example, try to imagine a cute bouncing tiger game with a survival horror soundtrack. Without that consistency be- tween the game play and the audio, users will find the game worlds harder to immerse themselves into.

Decompression of lots of sound effects can hog up the CPU, and streaming long audio files can cause jumps and stalls in your frame rate. Choosing the right audio for- mat can help, although Unity doesn’t really offer you much in the way of choice on that.

When preparing audio for use in a Unity iOS game, you should import uncom- pressed WAV files. That way, you start with a clean source and you can use the com- pression settings within Unity where required.

8.5.1 Sound Effects and Compression

For small sound effects, such as laser blasts or explosions, it is recommended that you do not use compressed audio files. Decompression is a mathematical process that re- quires calculation, and therefore CPU. If you are doing this every time the player shoots in a shoot ‘em up, the effect won’t take long to show up.

Although perhaps not an exhaustive list of techniques for dealing with sound ef- fects in Unity, here are a few common approaches.

1. Use the Audio.PlayOneShot function to play multiple sounds from the same audio source. This is great for player sounds such as jumping, dying, etc., where it will be unlikely for several sounds to play at the same time.

2. To use PlayOneShot, a GameObject that has a script attached to it must have an AudioSource component (from the Component–>Audio–>AudioSource menu). Whenever you need to play a sound from the script, you call

audio.PlayOneShot(anAudioClip);

3. In the case of missiles or objects that are generated on the fly (such as explo- sions), we can attach audio sources to them and begin playback of the sound effect automatically at the point of instantiation.

4. Instantiate audio GameObjects wherever the audio is required. That is, we in- stantiate empty GameObjects containing only an audio source and some kind of autodestructor to delete themselves after playback.

Using the GameObject.Instantiate function to generate objects at runtime during an iOS game is not recommended due to the performance overhead. Instead, I use option 1, which is using multiple audio sources and the Audio.PlayOneShot to make sounds. I normally have a GameObject in my scene for music and any additional audio emitters attached to the actual objects they are representing. If an object needs to make more than one sound, I add another emitter to the object.

8.5.2 Streaming Music

It is accepted that most games have background music. On a device such as the iPhone, this represents several problems: storage of a long audio track drives up your file size

8.5. Why Audio Can Make or Break Your Game 205

(and initial download size), large audio tracks are going to use up a chunk of your pre- cious memory space, and it takes up CPU to decompress audio as it plays, which can be detrimental to your frame rate.

Recommended practice is to try to stick to smaller audio clips, which may be looped to create seemingly longer music. It may even be possible to sequence audio clips ran- domly, so as to provide an interesting and dynamic soundtrack to your game (I’ve done something like this on several projects), or perhaps go so far as to change the loop based on what is happening in the game itself.

If you absolutely must use long audio clips, try to compress them as much as pos- sible and use the “streaming from disk” option in the import settings in Unity. Without streaming, your game will try to load the entire music file into memory before playing it, resulting in a huge stall as it loads everything in at the start of the scene, along with an unnecessary usage of memory.

8.5.3 What Settings Are There, and What Are Best to Use?

Unity supports a wide range of audio formats, such as .aif, .wav, .mp3, and .ogg, but with Unity iOS, the best option is to import all of your sounds as uncompressed .wav files and set them up so that the engine will take care of compressing or not compressing them. By using raw .wav files, you will gain the flexibility to experiment with different compression settings in the editor to find the best fit for your project. In Unity, you can choose how the device will play individual audio files, what their compression rates are, and how they will behave in the game environment.

To bring up the properties of an audio file, click on the file in the Project window of the Unity editor and the Inspector window will show the audio importer component and all of its options (Figure 8.4).

Audio format. This drop-down determines how the audio file is delivered to the iOS device. Unity iOS offers two options here: compressed (MPEG) or native (WAV).

When the compressed format is selected, although there are several alternative formats available to iOS developers, the Unity engine will compress your audio as .mp3 format files. This format uses significantly less RAM and disk space than the uncompressed alternative, the downside being that it takes more CPU work to de- compress the file. This setting is ideal for music and longer sounds (background audio loops, etc.).

Native (WAV) format is uncompressed, which means better quality audio and no decompression process required. The device’s CPU works considerably less to play the sound file than it will with a compressed file, however, the downside is that native files will use up a lot more RAM and disk space. This setting is best for short sounds, or sounds that will be played a lot during your game.

3D sound. This setting determines whether or not playback of this sound should be represented in a 3D space (as if it were being emitted from an object within the environ- ment). When a sound is set to 2D, it will sound as though it is close by and emitted from the speaker with no spatial coordination.

Force to mono. When this option is enabled, the audio file will be mixed down to a single- channel mono sample.

Load type. Unity iOS is capable of loading au- dio files in three different ways. Whichever one you choose will affect performance, and you should be sure to go through each sound file in a project.

y Decompress on load. This setting decom- presses compressed audio files at runtime.

It is useful for avoiding the performance overhead of decompressing on the fly, but it has the huge disadvantage of using up to 10 times more memory than when keeping them compressed. Certainly, this setting should be avoided for larger au- dio files (if not avoided completely) if you find you are pushing the RAM limits of the device.

y Compressed in memory. This setting keeps audio samples compressed in memory, which results in a small perfor- mance overhead during decompression when the sound is played. Unity recom- mends only using this setting for larger audio files to avoid a performance hit.

y Stream from disc. Selecting this option means that your audio file is streamed as it is played, taking up a much smaller amount of memory than if it were loaded into RAM. Unity advises the use of only 1 or 2 streams at any one time to avoid per- formance issues. This is ideally suited for use with a main music track or for a long background audio sound.

Hardware decoding. On iOS devices, Apple provides a hardware decoder for audio playback, which means much less work for the CPU during decompression.

Gapless looping. This option prepares the audio file to provide a loop with no inter- ruptions. A typical use for this might be a short audio sample that will loop to provide background music; without this gapless looping option checked, there may be a stall (or gap) in playback between the end of the sample and the start of it being replayed.

Figure 8.4. The audio importer within the Inspector window of the Unity editor.

Một phần của tài liệu Game Development for iOS with Unity3D 2012 (Trang 220 - 224)

Tải bản đầy đủ (PDF)

(278 trang)