I found this very simple timer script that would be ideal for a trip timer but I am wondering how to add a pause, stop and reset button to it?
#pragma strict
private var startTime : float;
var textTime : String;
//First define two variables. One private and one public variable. Set the first variable to be a float.
//Use for textTime a string.
function Start() {
startTime = Time.time;
}
function OnGUI () {
var guiTime = Time.time - startTime;
//The gui-Time is the difference between the actual time and the start time.
var minutes : int = guiTime / 60; //Divide the guiTime by sixty to get the minutes.
var seconds : int = guiTime % 60;//Use the euclidean division for the seconds.
var fraction : int = (guiTime * 100) % 100;
textTime = String.Format ("{0:00}:{1:00}:{2:00}", minutes, seconds, fraction);
//text.Time is the time that will be displayed.
GetComponent(GUIText).text = textTime;
}
Your script uses Time.time
which cannot be paused. So you may create your own timer instead, by adding Time.deltaTime
each frame.
This script allows you to play, pause and reset the timer:
#pragma strict
private var time : float;
var textTime : String;
var timerOn : boolean;
var buttonText : String;
function Start() {
timerOn = true;
buttonText = "Pause";
}
function Update(){
if(timerOn)
time += Time.deltaTime;
}
function OnGUI () {
var guiTime = time;
var minutes : int = guiTime / 60; //Divide the guiTime by sixty to get the minutes.
var seconds : int = guiTime % 60;//Use the euclidean division for the seconds.
var fraction : int = (guiTime * 100) % 100;
textTime = String.Format ("{0:00}:{1:00}:{2:00}", minutes, seconds, fraction);
//text.Time is the time that will be displayed.
GetComponent(GUIText).text = textTime;
if (GUI.Button(Rect(10,10,50,30), buttonText)){
timerOn = !timerOn;
if(timerOn) buttonText = "Pause";
else buttonText = "Play";
}
if (GUI.Button(Rect(70,10,50,30), "Reset")){
time = 0;
}
}
@zentaiguy, Try this…
private GUIStyle TestStyle = new GUIStyle ();
public Texture2D Pause_On = null;
public Texture2D Play_On = null;
public void OnGUI()
{
if (GUI.Button (new Rect (30, 950, Pause_On.width, Pause_On.height), Pause_On, TestStyle))
{
//Do your Stuff's Here...
}
if (GUI.Button (new Rect (30, 950, Play_On.width, Play_On.height), Play_On, TestStyle))
{
//Do your Stuff's Here...
}
}
If you satisfied +1 for me.