Game Development Reference
In-Depth Information
Engage Thrusters
In this secion, we will create the buton and script for the restart buton:
1. First, we need to create a simple
TextureButton
class to control our restart buton
(
Assets
|
Create
|
Javascript
) and name our script to
TextureButton
. Double-click
to open
MonoDevelop
and add the following code:
public var normalTexture : Texture2D;
public var rollOverTexture : Texture2D;
public var clickSound : AudioClip;
public var key : GameObject;
public var Player : GameObject;
From the preceding code, we have two
Texture2D
parameters,
normalTexture
and
rollOverTexture
, for the
restart
buton when it's in the rollout and rollover
state. We also have an audio to play a click sound FX.
2. Next, we have
key
and
Player
GameObject
, which we will assign the prefab of
key
and
Player
to use when the game is at an end. We are creaing a funcion to
change our restart buton texture when the user performs rollover and rollout. Add
the following script:
public function OnMouseEnter () : void { //Mouse Roll over
function
guiTexture.texture = rollOverTexture;
}
public function OnMouseExit() : void { //Mouse Roll out function
guiTexture.texture = normalTexture;
}
3. Now, we will create the funcion that will reset our character back to the start
posiion, and the key will appear in the scene again by using
Instatiate
to clone
our object from our prefab object in the projects, which we will create at a later state.
4. Finally, we have
@script RequireComponent(AudioSource)
to basically force
the script to add an
AudioSource
script to our
restartButton
, and to prevent
the error when we are running this script without the
AudioSource
script. Let's
add the following script:
public function OnMouseUp() : IEnumerator{ // Mouse up function
audio.PlayOneShot(clickSound);
yield new WaitForSeconds (1.0); //Wait for 0.5 secs. until do
the next function
//Create a new Player at the start position by cloning from our
prefab
Instantiate(Player, new Vector3(Player.transform.position.x,
Player.transform.position.y, 0.0), Player.transform.rotation);