unity3d之简单的时钟倒计时demo

输入结束时间,开始倒计时,时间差不超过一天,附上代码:(关于个位数显示,加个判断如果小于10 显示的字符串加上0)

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 using UnityEngine.UI;
 5 using System;
 6 public class CountdownDemo : MonoBehaviour {
 7 
 8     public Text hour;
 9     public Text min;
10     public Text sec;
11     public InputField timestr;
12     public Coroutine cc;
13     private DateTime endtime;
14 
15     
16     
17     IEnumerator countdown()
18     {
19         while (true)
20         {
21             TimeSpan temp = endtime - DateTime.Now;
22             hour.text = temp.Hours.ToString();
23             min.text = temp.Minutes.ToString();
24             sec.text = temp.Seconds.ToString();
25             yield return new WaitForSeconds(1);
26         }
27     }
28 
29     void OnGUI()
30     {
31         if (GUI.Button(new Rect(0, 0, 100, 30), "start"))
32         {
33             endtime = Convert.ToDateTime(timestr.text);
34             cc = StartCoroutine(countdown());
35         }
36         if (GUI.Button(new Rect(0, 100, 100, 30), "end"))
37         {
38             this.StopCoroutine(cc);
39         }
40     }
41 }

 

转载于:https://www.cnblogs.com/ninomiya/p/8856818.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值