简单计时器的制作

今天给大家带来的是计时器的简单制作,其中包含开始、暂停、结束按钮,显示格式为小时、分钟、秒。
首先创建一个场景和一个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;
}
}
}

智慧城市受欢迎的理由是什么
本田新款CR-V被曝光到底新在哪?看完这个再买不迟!
供应/回收HP8903失真分析仪HP8903E 小兵/罗S
用于神经场SLAM的矢量化对象建图
云+时代下VR全景行业迎来了爆发期
简单计时器的制作
Zigbee模块的功能/硬件组成/软件组成
Light Conference2019国际会议盛大开幕邀请18个国家50余位知名家
主打嵌入式应用的中高阶RTOS
一文浅析仪器仪表的精度
时序逻辑电路
柔性电池可进行弯曲和扭曲,可能是未来柔性设备理性选择
水芯电子成为移动电源国家标准修订项目组成员单位
首份全国网民3G调查报告发布
如何设计一款简单的移动支付读卡器
烽火亮相第二届产业智能化应用创新推进大会
土壤测定仪的使用方法及技术参数
互联网的技术应用将引领智慧工地快速发展
一段过流、二段过流、三段过流
什么是双极型三极管