Advertisemen
Let's take a look, how to add images to show count down. First, create some images like 0, 1, 2, 3 in photoshop or others and drag them to unity editor.
Now, open up unity and create one C# Script and name it whatever you want and then attached it to camera.
Let's edit script, type the followings -
using UnityEngine;
using System.Collections;
public class countDown : MonoBehaviour {
public Texture2D img0;
public Texture2D img1;
public Texture2D img2;
public Texture2D img3;
private int totalTime;
private float initialTime;
void Awake(){
totalTime = 3;
initialTime = Time.time;
}
void OnGUI(){
GUILayout.Label( images() );
}
int TimeRemaining(){
int elapsedTime = (int)(Time.time - initialTime);
"color: #f7f7ec;"> int timeLeft = (totalTime - elapsedTime);
return timeLeft;
}
Texture2D images(){
switch(TimeRemaining() ){
case 3:
return img3;
case 2:
return img2;
case 1:
return img1;
default:
return img0;
}
}
}
As you can see, we have created Texture2D variables to hold the images we created earlier. Inside, Awake function, we have totalTime = 3; which means we are counting down from 3, if you want to start count down from 5, go ahead and type 5 but remember you have to have 5 images.And initialTime = Time.time; what this one does is holds the time value on Awake of the scene.Function TimeRemaining() returns the time left after deducting difference of Time.time on Awake and Time.time started later.
Drag the images to image place holder 1, 2, 3 respectively.
Now, go ahead and run, you will see counting down...
return timeLeft;
}
Texture2D images(){
switch(TimeRemaining() ){
case 3:
return img3;
case 2:
return img2;
case 1:
return img1;
default:
return img0;
}
}
}
As you can see, we have created Texture2D variables to hold the images we created earlier. Inside, Awake function, we have totalTime = 3; which means we are counting down from 3, if you want to start count down from 5, go ahead and type 5 but remember you have to have 5 images.And initialTime = Time.time; what this one does is holds the time value on Awake of the scene.Function TimeRemaining() returns the time left after deducting difference of Time.time on Awake and Time.time started later.
Drag the images to image place holder 1, 2, 3 respectively.
Now, go ahead and run, you will see counting down...
Advertisemen
Tidak ada komentar:
Posting Komentar