Unity3d for iOS Beginner Tutorial: Part 3/3 (bottom)

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/alongu3d/archive/2013/06/01/3111739.html

Messages are not automatically eliminated
Your basic gameplay has been completed, but it shows some information about the game when it first loads is a good highlight. You now all show a play button. Users don't know what they're going to do. Adding some welcome text and a super brief introduction to the game will make it more user-friendly.:] Welcome text will use you to import Transformers fonts. For introductory text. You'll use Arial fonts packaged with unity. Create an empty game object, name it intro, and then set Transform position to 0,0,0. Select Gameobjectcreate Other3d text to create a 3D text gameobject, and then name it description text. Set the Inspectortext Meshtext property as "Get to the finish line before time runs out". Set the initial transform position to 10, 1, 12.

Use the Unity editor to play the game, then adjust the position of the object so that it is centered horizontally so that you can see it clearly.

Tip: You'll have to deal with X and Z positions: X is used to align objects in the middle, and Z is used to zoom in and out.

<a href= "Http://www.raywenderlich.com/wp-content/uploads/2012/11/part3_39b_desc_text_adjusted.png" >< IMG title= "Description text position adjustments." Src= "http://www.raywenderlich.com/wp-content/uploads/2012/11/ Part3_39b_desc_text_adjusted-700x297.png "alt=" Description text position adjustments. "Width=", "height=", 297 "/>" </a> check the game with unity remote to make sure the text appears on your iOS device. Some of the necessary adjustments. Place the description text below the intro gameobject. The purpose of this is that you can show or hide the menu information by following the simple code.

Create a second 3D text gameobject, and then name it welcome text. This text should appear above the descriptive text, so set the initial transform position to -6,5,10. Set the Inspectortext Meshtext property to "Welcome". Set the Font property to Transformers Movie by dragging the Font property in the Project view to Inspector (or by tapping the circle with dots on the font edge and selecting the font from the list that pops up): <a href= "http:/ /www.raywenderlich.com/wp-content/uploads/2012/11/part3_42b_welcome_text_transform_text_font.png "></a>" adjusts the location of the welcome text so you can see clearly when you test the game in Unity Editor and Unity remote. <a href= "Http://www.raywenderlich.com/wp-content/uploads/2012/11/part3_43b_welcome_text_adjusted.png" > </a> Place Welcome text object under Intro gameobject. When the game starts running, you want to hide the Introgameobject (and its sub-objects). 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 the handle of the intro gameobject. Then you modify Startgame () to make intro gameobject invisible by turning off its child gameobject rendering. Set the Controllerintro variable now by selecting the main camera and then dragging the intro gameobject from the hierarchy view to the inspectorgame intro variable. or use the circle dot icon because it's easier.:]

Create a second 3D text gameobject, and then name it welcome text. This text should appear above the descriptive text, so set the initial transform position to -6,5,10. Set the Inspectortext Meshtext property to "Welcome".

Set the Font property to Transformers Movie by dragging the Font property in the Project view to Inspector (or by tapping the circle with dots on the font edge and selecting the font from the pop-up list):

Adjust the location of the welcome text so you can see it clearly when you test the game in Unity Editor and Unity remote.

Place the Welcome text object below the intro gameobject.

When the game starts running, you want to hide the Introgameobject (and its sub-objects). 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 the handle of the intro gameobject. Then you modify Startgame () to make intro gameobject invisible by turning off its child gameobject rendering.

Set the Controllerintro variable now by selecting the main camera and then dragging the intro gameobject from the hierarchy view to the inspectorgame intro variable. or use the circle dot icon because it's easier.:]

Demo game to test when the play button is clicked, the text is hidden after the game starts.

Every brave little square needs a track.

Music plays an important role in the experience of the game and provides sensory feedback to create emotions. You will be adding audio to enhance gameplay.

The sound effect is triggered when the player passes the finish line on time, or their task fails, or the obstacle hits the ground, or touches anything. Of course, the game also needs some background music!:]

Adding audio with unity involves associating an audio source component onto a gameobject. This audio source component has an audio clip property that allows you to assign the sound you want to play. This component has additional properties to control whether the sound is playable or can be looped. Supported audio formats include. AIF,. Wav. MP3, and. OGG.

The following two websites offer free music that can be used in this tutorial:

    • http://incompetech.com/
    • http://freesound.org/(download sound requires registration)

The Resources.zip file you downloaded earlier includes all the audio files you will use. You are free to create your own sound to replace those I offer to you. :]

As your reference (and in order to find the original link), the original link to the audio file contained in the Resources.zip file is as follows:

    • Victory: 20784__wanna73__crowd-cheering-football-01.wav
    • Failure: 83916__timbre__benboncan-sad-trombone-tweaked.wav
    • Collision: 30668__hardpcm__hardbassdrum002.wav
    • Background: Gustav Sting.mp3

Note that in the Resources.zip file, for clarity and brevity, the file has been renamed. Go to the folder where you initially unzipped the Resources.zip, and then import the audio file by dragging them into your project Viewassets folder.

When an audio file is imported into unity, you can specify whether to compress or maintain the status quo, such as natural state (but note that MP3 and Ogg Vorbis audio are always imported in a compressed format)

Why is that? Compressed files are smaller, but they need to be decompressed while the game is running, consuming CPU cycles. You usually want to decompress the background music. For short sound effects, the natural state will be better and can provide better sound effects.

If the audio format is compressed, you can choose whether to handle the decompression via hardware, for example, Apple's hardware decoder if it is running on an iOS device. The hardware will be faster, but the hardware can only process one compressed file at a time.

You can also make the sound have a effect. This means that when the sound is played, the effect is correlated with the 3D position of the gameobject. For example, if gameobject away from you, the sound will become lighter.

Select the background music in the project view to display the imported settings. Do not tick the 3D sound option. Select Hardware decoding. Click Apply to save the changed settings.

Some other audio files are. WAV file, you do not need to change the default import settings, that is, the audio format that should be set to both the and natural.

For the sound you hear, your scene needs an audio listener component added to a gameobject. There is only one audio Listener in the scene. This listener picks up the sound from the source closest to it and sends it to the device's horn.

By default, the Audio listener is associated to the main camera. You can put him there or associate it to a different gameobject: for example, on your character.

In this game, you keep the audio listener on the main camera, but you can feel the different options when you build your own game.

The sound of Victory and defeat

You intend to correlate the audio to goal gameobject to simulate a cheering or mocking audience at the finish line.

Select Goal Gameobject in the hierarchy view and add an audio source by selecting Componentaudioaudio source. Set the Victory audio resource to the Inspectoraudio Sourceaudio clip property. Do not tick the option to play in awake.

Now create a new JavaScript resource to play as a winning sound or a failed sound. Name this new script for fanreaction. Open the new script, remove the automatically added function, and add the following code:

var audiovictory:audioclip;var audiodefeat:audioclip;var volumevictory:float = 2.0;var Volumedefeat:float = 2.0;fu Nction playsoundofvictory (Isvictory:boolean) {    //Stop Any current audio    if (audio.isplaying)        audio. Stop ();    Play either the sound of victory or defeat.    Audio.clip = isvictory? Audiovictory:audiodefeat;    Audio.volume = isvictory? Volumevictory:volumedefeat;    Audio. Play ();} function Resetgame () {    //Reset to original state, stop any audio    if (audio.isplaying)        audio. Stop ();} @script requirecomponent (Audiosource)

This script handles two segments of audio, one for victory and the other for failure. The Playsoundofvictory function first stops playing any currently playing sound, and then plays the desired sound according to the isvictory input.

The Resetgame () function stops playing any sound that is playing. You'll think of Gamecontroller calling Resetgame () every time the game starts again.

Associate this new script to goal gameobject. Set the Victory audio resource to the audio victory variable. Set the defeat audio resource to the audio defeat variable.

Edit the Gamecontroller script and make the following changes:

var fanreactionscript:fanreaction;...function Update () {     if (!gamerunning)        return;     Keep track of time and display a countdown    gametimeremaining-= time.deltatime;    if (gametimeremaining <= 0) {        timedout = true;         Gamerunning = false;        Play the sound of Defeat        Fanreactionscript.playsoundofvictory (false);}    } ... function missioncomplete () {     if (!gamerunning)        return;    Missioncompleted = true;     Gamerunning = false;    Play The sound of Victory    Fanreactionscript.playsoundofvictory (true);    Missioncompletetime =  gametimeallowed-gametimeremaining;}

The code defines a new public variable to reference the Fanreaction script. You modify Missioncomplete () to call Playsoundofvictory, passing true to play the winning sound. You also need to modify update () to call Playsoundofvictory () and pass false to play the failed sound.

The Gamecontroller script component of the main camera goal gameobject with a variable to connect the Fanreaction script. Select the main camera in the inspector, and then click the Circle point on the edge of the Fanreactionscript variable below the Gamecontroller component. In the dialog box that pops up, select Goal Gameobject, and then close the popup dialog box.

To invoke Resetgame () in Fanreaction, select the main camera object. In the Inspector game Controller component column, add the game object to the reset array, from 2 to 3. Set goal Gameobejct to element 2.

Try the game and test the winning and failing scenes to make sure the game plays the right sound. Check to see if the sound is stopped when you click on the play button again.

A thud in a 3D environment

When obstacles hit the ground, there is a sound that sounds good. To do this, you associate an audio source to the obstacle prefab, and then when the obstacle falls or collides with anything else, you can monitor the collision to play the sound of the collision.

Add an audio component to the obstacle precast parts. Assign impact audio to the audio clip property.

Create a new JavaScript script resource, and then name it objectcollision. Edit the script, delete the pre-added function, and add the following code:

var impact:audioclip;function oncollisionenter () {    audio. Playoneshot (impact);} @script requirecomponent (Audiosource)

This code implements a pre-defined Oncollisionenter event function that calls audio. Playoneshot () function to play the hit audio. Audio. Playoneshot () shows another way to play audio, allowing you to pass the audio you want to play.

Associate the script to the obstacle prefab. Set the impact audio resource to the impact variable in the script.

Try the game and check if you hear a welcome bang when the obstacles hit the ground or other objects. Note the closer the player is to the obstacle, the louder the sound.

Small squares of music

Your game is almost finished. But there is one more thing missing – some background music.

It creates a lot of atmosphere for game music. It allows the user's adrenaline to flow and helps them provide clues to the game environment through the sounds of birds or the howling of wolves. So add some music!

Add an audio source component to the main camera. Set the background audio resource to the audio clip property.

Tick the play on Awake and loop properties. This ensures that the background music starts playing when the game starts and continues playing.

Adjust the volume from 0.1 until it has not drowned out other sounds. If you use your own sound, fine-tune the volume level according to your music default volume level to accomplish the same goal. The user should be able to channel all sound effects when the background music is playing.

Try this project in the Unity editor. When completely satisfied, deploy this project to your iOS device. You need to add the level_3 scene in the Build settings.

Test the entire game on your iOS device until you're satisfied with the sound you added.

Your heroic little square has a background music to inspire its morale.

Where are you headed?

Congratulations, you've reached the end of the Unity Foundation Whirlwind ACT! You have shown the vigor and vitality of your heroic little cube. This may be the beginning of a wonderful tour of unity games.

Here is the source file for all the code in this series of tutorials: Unity project, Xcode project.

Believe it or not, you just know a little fur – there's a lot to learn. Stay tuned for an intermediate tutorial series that will take you to the next level of unity.

Unity3d for iOS Beginner Tutorial: Part 3/3 (bottom)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.