Today, we bring you a simple production of timer, including start, pause and end buttons. The display format is hour, minute and second.

First, create a scenario and a c# script

Production of simple timer

Click the camera main camera in the hierarchy layout to hang the script on the camera and drag it directly to the camera or drag it to the add component in the lower right corner to complete the script binding.

Open c# script:

Create five global variables outside the method

string timerRet = “”;

float TImer = 0;

float TImerDel = 0;

int hour = 0;

int minute = 0;

int second = 0;

The face change in this method is constantly changing, so the whole process is executed in the void update() method:

void Update () {

//Timer indicates the recorded time period + = time.deltatime timing accumulation

timer += timerDel;

//Determine the number of seconds and divide them into minutes and seconds

if (timer 》 1)

{

second++;

timer -= 1;

}

if (second 》= 60)

{

minute++;

second = 0;

}

if (minute 》= 60)

{

hour++;

minute = 0;

}

//Timerret is the string presented on the display. Rewrite here and write the time

timerRet = string.Format(“{0:00}:{1:00}:{2:00}”, hour, minute, second);

}

Set the button and display in the void ongui() method:

//Design a string variable to change pause and resume

Static string goon = “pause”;

void OnGUI()

{

//Gui.label display

GUI.Label(new Rect(150, 190, 200, 150), timerRet);

//Gui.button press new rect to set the position (x-axis, y-axis, length, width) and content

If (GUI. Button (New Rect (0, 0120100), “start”))

{

//Time.deltatime is the incremental time, which is assigned to timerdel for accumulation

timerDel = Time.deltaTime;

}

//A string that makes changes to resume and pause

string suspend = “”;

suspend = string.Format(“{0}”, goOn);

if (GUI.Button(new Rect(0, 150, 120, 100),suspend))

{

//Click “pause” and replace with “continue”

Goon = “continue”;

if (timerDel == 0)

{

//Click “continue” and replace with “pause”

Goon = “pause”;

timerDel = Time.deltaTime;

}

else

{

timerDel = 0;

}

}

//Zero the variable and the timing ends

If (GUI. Button (New Rect (0300120100), “end”)

{

hour = 0;

minute = 0;

second = 0;

timerDel = 0;

}

Well, a simple timer script is completed. Is it very simple?

Click the start button to display the time walk, pause, time stop, the pause key becomes the continue key, click the time continue again, click the end button, and the time returns to zero.

Production of simple timer

Making of simple stopwatch timer

The functions of this simple timer are as follows:

1. Click start for timing. At this time, the start button and the stop and reset buttons operate normally.

2. Click the stop button to stop the timing. At this time, the stop button and the start and reset buttons operate normally.

3. Click the reset button to return to the start timing initial interface regardless of the current timing state or stop state.

Production of simple timer

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

namespace Exercise

{

Public partial class timer: form

{

private DateTime timeSum;

Public timer ()

{

InitializeComponent();

}

private void Form1_ Load(object sender, EventArgs e)

{

this.timeSum = new DateTime(0); // Start display time 0.0.0:0

this.label1.Text = timeSum.Hour + “。” + timeSum.Minute + “。” + timeSum.Second + “:” + timeSum.Millisecond;

this.button2.Enabled = false;

}

private void IncreaseTime(double seconds)

{

this.timeSum = this.timeSum.AddSeconds(seconds);

this.label1.Text = timeSum.Hour + “。” + timeSum.Minute + “。” + timeSum.Second + “:” + timeSum.Millisecond;

}

private void timer1_ Tick(object sender, EventArgs e)

{

this.IncreaseTime(0.1);

}

private void button1_ Click(object sender, EventArgs e)

{

this.timer1.Start();

this.button1.Enabled = false;

this.button2.Enabled = true;

}

private void button2_ Click(object sender, EventArgs e)

{

this.timer1.Stop();

this.button1.Enabled = true;

this.button2.Enabled = false;

}

private void button3_ Click(object sender, EventArgs e)

{

this.timeSum = new DateTime(0); // Start display time 0.0.0:0

this.timer1.Stop();

this.label1.Text = timeSum.Hour + “。” + timeSum.Minute + “。” + timeSum.Second + “:” + timeSum.Millisecond;

this.button1.Enabled = true;

this.button2.Enabled = true;

}

}

}

Leave a Reply

Your email address will not be published. Required fields are marked *