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.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Beginning Unity 3D for iOS: Part 3/3
50 mins
- Getting Started: The End in Sight!
- Connect the Dots
- Create the Launcher
- Create the Obstacles
- Release the Krak… Err, Obstacles
- The Final Countdown
- Sometimes You Win, Sometimes You Lose
- Transformer-ing the Display Font
- It’s Always Play Time!
- Every Cube Deserves a Fresh Start
- This Message Won’t Self-Destruct
- Every Brave Cube Deserves a Soundtrack
- The Sounds of Victory and Defeat
- Thud in 3D
- A Little Cube Music
- Where To Go From Here?
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.
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.
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.
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.
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):
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.
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. :]
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:
- http://incompetech.com/
- http://freesound.org/ (registration required to download sounds)
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:
- victory: 20784__wanna73__crowd-cheering-football-01.wav
- defeat: 83916__timbre__benboncan-sad-trombone-tweaked.wav
- impact: 30668__hardpcm__hardbassdrum002.wav
- background: Gustav Sting.mp3
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.
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.
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.