简单计时器的制作

简单计时器的制作,第1张

今天给大家带来的是计时器的简单制作,其中包含开始、暂停、结束按钮,显示格式为小时、分钟、秒。

首先创建一个场景和一个C#脚本

点击 Hierarchy 版面的 摄像机 Main Camera,可将脚本挂在摄像机上,直接拖拽到摄像机或者拖拽到右下角的 Add Component处完成脚本绑定。

打开C#脚本:

在方法外创建五个全局变量

string TImerRet = “”;

float TImer = 0;

float TImerDel = 0;

int hour = 0;

int minute = 0;

int second = 0;

此方法中的变脸在持续变化,因此全程在void Update ()方法执行:

void Update () {

//TImer表示计录的时间段 += Time.deltaTime 计时累加

timer += timerDel;

//判定秒数,进行分秒分割

if (timer 》 1)

{

second++;

timer -= 1;

}

if (second 》= 60)

{

minute++;

second = 0;

}

if (minute 》= 60)

{

hour++;

minute = 0;

}

//timerRet 为呈现在显示器上的字符串,在此进行重写,写入时间

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

}

在 void OnGUI() 方法中进行按钮及显示设定:

//设计一个字符串变量用于改变 暂停 和 继续

static string goOn = “暂停”;

void OnGUI()

{

//GUI.Label 显示

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

//GUI.Button 按键 new Rect 设定位置(X轴,Y轴,长度,宽度),内容

if (GUI.Button(new Rect(0, 0, 120, 100), “开始”))

{

//Time.deltaTime 为增量时间 赋值给 timerDel进行累加

timerDel = Time.deltaTime;

}

//对继续和暂停进行更改的字符串

string suspend = “”;

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

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

{

//点击“暂停”完毕后用“继续”替换

goOn = “继续”;

if (timerDel == 0)

{

//点击“继续”完毕后用“暂停”替换

goOn = “暂停”;

timerDel = Time.deltaTime;

}

else

{

timerDel = 0;

}

}

//将变量归零,计时结束

if (GUI.Button(new Rect(0, 300, 120, 100), “结束”))

{

hour = 0;

minute = 0;

second = 0;

timerDel = 0;

}

好啦,一个简单的计时器的脚本就完成了,是不是很简单呢?

点击开始按钮,显示时间走动,暂停,时间停止走动,暂停键变成了继续键,再次点击时间继续,点击结束按钮,时间归零。

简单秒表计时器的制作

这个简单计时器的功能如下:

1、点击开始,进行计时,此时开始按钮灰度,停止和重置按钮正常运行。

2、点击停止按钮,计时停止,此时停止按钮灰度,开始和重置按钮正常运行。

3、点击重置按钮,无论当前是计时状态还是停止状态,均恢复至开始计时初始界面。

简单计时器的制作,简单计时器的制作,第2张

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 计时器 : Form

{

private DateTime timeSum;

public 计时器()

{

InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)

{

this.timeSum = new DateTime(0); //开始显示时间 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); //开始显示时间 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;

}

}

}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/dianzi/2559886.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-08-06
下一篇 2022-08-06

发表评论

登录后才能评论

评论列表(0条)

保存