Simple clock countdown demo of unity3d

Enter the end time, start the countdown, the time difference is not more than one day, and attach the code:

 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 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324491647&siteId=291194637