Beginning Unity 3D for iOS: Part 3/3

This is a post by Tutorial Team Member Christine Abernathy, an Engineer on the Developer Advocacy team at Facebook. You can also find her on Google+. Welcome to the third and final part the Beginning Unity 3D for iOS tutorial series! In the first part of this series, you toured the basic Unity tools, created […] By Christine Abernathy.

Leave a rating/review
Save for later
Share
You are currently viewing page 5 of 6 of this article. Click here to view the first page.

This Message Won’t Self-Destruct

Your basic gameplay functionality is now complete, but it would be a nice additional touch to show some info about the game when it's first launched. All you're currently showing is a play button. The user doesn’t know what they’re in for.

Adding some welcome text and a super-short explanation of what the game's about will make it a lot more user-friendly. :] The welcome text will use the Transformers font you imported. For the description text, you’ll use the Arial font that is bundled with Unity.

Create an empty game object, name it Intro and set the Transform Position to 0,0,0.

Select GameObject\Create Other\3D Text to create a 3D Text GameObject and name it Description Text. Set the Inspector\Text Mesh\Text property to "Get to the finish line before time runs out." Set the initial Transform Position to -10,1,12.

Description text adjustments.

Preview the game with the Unity Editor and adjust the position of the object so that it's horizontally centered and you can see it clearly.

Hint: You'll likely have to play around with the x and z positions: x to center to object, and z to zoom in and out.

Description text position adjustments.

Check out the game with Unity Remote as well and make sure the text is visible when viewed on an iOS device. Make any necessary adjustments.

Place the Description Text object under the Intro GameObject. You’re doing this so you can later show or hide the menu display info easily via code.

Description text parented.

Create a second 3D Text GameObject and name it Welcome Text. This text should appear above the description text, so set the initial Transform Position to -6,5,10. Set the Inspector\Text Mesh\Text property to "Welcome".

Set the Font property to Transformers Movie by dragging that font asset from the Project View and into the Font property in the Inspector (or by tapping the circle with a dot icon next to Font and selecting it from the pop-up list):

Welcome text transform and material modifications.

Adjust the position of Welcome Text so that you can see it clearly when you test the game on both the Unity Editor and through Unity Remote.

Welcome text position adjustments.

Place the Welcome Text object under the Intro GameObject.

You want to hide the Intro GameObject (and its child objects) when the game is running. Open the GameController script and make the following changes:

var intro : Transform;
...
function startGame() {
...
    // Turn off the intro text
    for (var child : Transform in intro ) {
        child.gameObject.renderer.enabled = false;
    }
    
    // Clean out any enemy objects
...

Here you add a new public variable to get a handle to the Intro GameObject. Then you modify startGame() to make the Intro object invisible by turning off the renderers for its child GameObjects.

Now set the Intro variable by selecting Main Camera and dragging the Intro GameObject from the Hierarchy View to the Inspector\Game Controller\Intro variable to assign it. Or use the circle dot icon, since it's easier. :]

Intro transform assigned to Game Controller script.

Preview the game to test that the text is hidden when the play button is clicked and the game begins.

Every Brave Cube Deserves a Soundtrack

Audio plays a big part in the gaming experience, both by providing sensory feedback and creating mood. You'll add audio to further enrich the gameplay.

Sound effects will be triggered when the player crosses the line in time, fails their mission, or when an obstacle hits the ground or bumps into anything. And of course, the game must have some background music! :]

Adding audio with Unity involves attaching an Audio Source component to a GameObject. The Audio Source component has an Audio Clip property that you can assign to the sound you wish to play. The component has additional properties that can control when the clip should be played and whether it should loop. The supported audio file formats include .AIF, .WAV, .MP3, and .OGG.

The following two sites provided the royalty-free music that's used in this tutorial:

The Resources.zip file you downloaded earlier contains all the audio files that you'll be using. Feel free to create your own audio effects instead of using the ones I've provided. :]

For your reference (and for the sake of attribution), the original links to the audio files included in the Resources.zip file are as follows:

Do note though that the files in the Resources.zip file have been renamed for the sake of clarity and brevity. Go to the folder where you originally extracted Resources.zip and import the audio files by dragging them into your Project View\Assets folder.

Victory audio import settings.

When an audio file is imported into Unity, you can specify whether it should be compressed or remain as-is, i.e., native. (But note that MP3 and Ogg Vorbis audio are always imported in the compressed format.)

Why does this matter? Compressed files tend to be smaller, but they need to be decompressed as the game runs, taking up CPU cycles. You generally want to compress background music. For short sound effects, native is better and tends to provide better sound quality.

If the audio format is compressed, you can choose whether to handle the decompression using hardware, e.g., Apple's hardware codec if running on an iOS device. Hardware is faster, but the hardware can handle one compressed music file at a time.

You can also mark sounds as 3D. This means that when the sound is played, the effect will be relative to the 3D position of the GameObject. For example, if the GameObject is far away, the sound will be quieter.

Background audio import settings.

Select the background audio in the Project View to show the Import Settings. Unselect the 3D Sound option. Select Hardware decoding. Click Apply to save the setting changes.

The other audio files are .WAV files and you do not need to modify the default Import settings, which should be set to 3D and native audio format.

For sounds to be heard, your scene needs an Audio Listener component to be added to a GameObject. There can only be one Audio Listener in the scene. The listener will pick up sounds from the audio sources close to it and send it to the device speaker.

By default, an Audio Listener is attached to the Main Camera. You can leave it there or attach it to a different GameObject: the player, for example.

In this game, you'll keep the Audio Listener on the Main Camera, but you can experiment with the different options when you build your own games.

Christine Abernathy

Contributors

Christine Abernathy

Author

Over 300 content creators. Join our team.