简单计时器的制作

控制/MCU

1799人已加入

描述

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

首先创建一个场景和一个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、点击重置按钮,无论当前是计时状态还是停止状态,均恢复至开始计时初始界面。

脚本

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;

}

}

}

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分