上班一个月~上周被项目虐了,功能实现有点bug,怎么都调不好,搞得心情都没那么愉快了。还好问了一下同事,然后某天突然来了灵感,早上一去就解决了~好吧,感觉敲代码也是需要灵感滴
前几天用到一个倒计时,写了功能,来记录一下~
上代码:
using UnityEngine; using System.Collections; public class daojishi : MonoBehaviour { float CoolTimes = 10000; GUIText time; void Start() { time = GameObject.Find("Time").GetComponent<GUIText> (); } void OnGUI() { if (CoolTimes >= 3600) { time.text = (Mathf.Floor(CoolTimes / 3600) + ":" + Mathf.Floor((CoolTimes - 3600 * Mathf.Floor(CoolTimes / 3600)) / 60) + ":" + CoolTimes % 60).ToString(); } else { time.text = (Mathf.Floor(CoolTimes / 60) + ":" + CoolTimes % 60).ToString(); } if (GUILayout.Button("Start")) { StartCoroutine(Wait()); } } IEnumerator Wait() { while (CoolTimes > 0) { yield return new WaitForSeconds(1f); CoolTimes--; } } }