- Home /
Stop and Pause Timer
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;
}
Answer by ahmedbenlakhdhar · Dec 04, 2014 at 10:32 PM
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;
}
}
$$anonymous$$an that's awesome ahmedbenlakhdar ($$anonymous$$an that was a mouth full ;) Thanks this is exactly what I was looking all over hells half acre for :) :) :)
One more quick question, how would I change the simple GUI buttons to a couple of 2 state GUI texture buttons?
Answer by Prasanna · Dec 05, 2014 at 07:25 AM
@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.
I have been using this type of convention for most of my buttons so I'm a little confused how I make that work with this type of button configuration?
var mouseUpTexture : Texture2D;
var mouseDownTexture : Texture2D;
var beep : AudioClip;
function On$$anonymous$$ouseDown(){
guiTexture.texture = mouseDownTexture;
audio.PlayOneShot(beep);
}
function On$$anonymous$$ouseUp(){
guiTexture.texture = mouseUpTexture;
yield new WaitForSeconds(0.3);
//Do Something
}
Basically I have been using one short script like this per button.
Still trying to figure out how to get this to work with my current GUI buttons configuration???