今天給大家?guī)淼氖怯?jì)時(shí)器的簡單制作,其中包含開始、暫停、結(jié)束按鈕,顯示格式為小時(shí)、分鐘、秒。
首先創(chuàng)建一個(gè)場景和一個(gè)C#腳本
點(diǎn)擊 Hierarchy 版面的 攝像機(jī) Main Camera,可將腳本掛在攝像機(jī)上,直接拖拽到攝像機(jī)或者拖拽到右下角的 Add Component處完成腳本綁定。
打開C#腳本:
在方法外創(chuàng)建五個(gè)全局變量
string timerRet = “”;
float timer = 0;
float timerDel = 0;
int hour = 0;
int minute = 0;
int second = 0;
此方法中的變臉在持續(xù)變化,因此全程在void Update ()方法執(zhí)行:
void Update () {
//timer表示計(jì)錄的時(shí)間段 += Time.deltaTime 計(jì)時(shí)累加
timer += timerDel;
//判定秒數(shù),進(jìn)行分秒分割
if (timer 》 1)
{
second++;
timer -= 1;
}
if (second 》= 60)
{
minute++;
second = 0;
}
if (minute 》= 60)
{
hour++;
minute = 0;
}
//timerRet 為呈現(xiàn)在顯示器上的字符串,在此進(jìn)行重寫,寫入時(shí)間
timerRet = string.Format(“{0:00}:{1:00}:{2:00}”, hour, minute, second);
}
在 void OnGUI() 方法中進(jìn)行按鈕及顯示設(shè)定:
//設(shè)計(jì)一個(gè)字符串變量用于改變 暫停 和 繼續(xù)
static string goOn = “暫?!?
void OnGUI()
{
//GUI.Label 顯示
GUI.Label(new Rect(150, 190, 200, 150), timerRet);
//GUI.Button 按鍵 new Rect 設(shè)定位置(X軸,Y軸,長度,寬度),內(nèi)容
if (GUI.Button(new Rect(0, 0, 120, 100), “開始”))
{
//Time.deltaTime 為增量時(shí)間 賦值給 timerDel進(jìn)行累加
timerDel = Time.deltaTime;
}
//對(duì)繼續(xù)和暫停進(jìn)行更改的字符串
string suspend = “”;
suspend = string.Format(“{0}”, goOn);
if (GUI.Button(new Rect(0, 150, 120, 100),suspend))
{
//點(diǎn)擊“暫?!蓖戤吅笥谩袄^續(xù)”替換
goOn = “繼續(xù)”;
if (timerDel == 0)
{
//點(diǎn)擊“繼續(xù)”完畢后用“暫?!碧鎿Q
goOn = “暫?!?
timerDel = Time.deltaTime;
}
else
{
timerDel = 0;
}
}
//將變量歸零,計(jì)時(shí)結(jié)束
if (GUI.Button(new Rect(0, 300, 120, 100), “結(jié)束”))
{
hour = 0;
minute = 0;
second = 0;
timerDel = 0;
}
好啦,一個(gè)簡單的計(jì)時(shí)器的腳本就完成了,是不是很簡單呢?
點(diǎn)擊開始按鈕,顯示時(shí)間走動(dòng),暫停,時(shí)間停止走動(dòng),暫停鍵變成了繼續(xù)鍵,再次點(diǎn)擊時(shí)間繼續(xù),點(diǎn)擊結(jié)束按鈕,時(shí)間歸零。
簡單秒表計(jì)時(shí)器的制作
這個(gè)簡單計(jì)時(shí)器的功能如下:
1、點(diǎn)擊開始,進(jìn)行計(jì)時(shí),此時(shí)開始按鈕灰度,停止和重置按鈕正常運(yùn)行。
2、點(diǎn)擊停止按鈕,計(jì)時(shí)停止,此時(shí)停止按鈕灰度,開始和重置按鈕正常運(yùn)行。
3、點(diǎn)擊重置按鈕,無論當(dāng)前是計(jì)時(shí)狀態(tài)還是停止?fàn)顟B(tài),均恢復(fù)至開始計(jì)時(shí)初始界面。
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 計(jì)時(shí)器 : Form
{
private DateTime timeSum;
public 計(jì)時(shí)器()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.timeSum = new DateTime(0); //開始顯示時(shí)間 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); //開始顯示時(shí)間 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;
}
}
}
評(píng)論
查看更多