Unity 秒表的实现 / 倒计时的实现
下面直接上代码
1.计时器的实现 第一种方法
/// <summary> /// 秒表的实现方法 创建一个Text 挂在上面即可 /// </summary> private float timeSpend = 0; private int hour; //小时 private int minute; //分钟 private int second; //秒 private int millisecond; //毫秒 private Text recealText; //显示的text private void Start() { recealText = this.transform.GetComponent<Text>(); } // Update is called once per frame void Update() { timeSpend += Time.deltaTime; hour = (int)timeSpend / 3600; minute = ((int)timeSpend - hour * 3600) / 60; second = (int)timeSpend - hour * 3600 - minute * 60; millisecond = (int)((timeSpend - (int)timeSpend) * 1000); recealText.text = string.Format("{0:D2}:{1:D2}:{2:D2}:{3:D3}", hour, minute, second, millisecond); }
第二种方法实现
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Timer_Clock : MonoBehaviour { /// <summary> /// 秒表的实现方法 创建一个Text 挂在上面即可 /// </summary> private float timeSpend = 0; private int hour; //小时 private int minute; //分钟 private int second; //秒 private int millisecond; //毫秒 public Text recealText; //显示的text public bool mator = false; private void Start() { recealText = this.transform.GetComponent<Text>(); StartCoroutine(timerr ()); } IEnumerator timerr() { while (!mator) { timeSpend += Time.deltaTime; hour = (int)timeSpend / 3600; minute = ((int)timeSpend - hour * 3600) / 60; second = (int)timeSpend - hour * 3600 - minute * 60; millisecond = (int)((timeSpend - (int)timeSpend) * 1000); recealText.text = string.Format("{0:D2}:{1:D2}:{2:D2}:{3:D3}", hour, minute, second, millisecond); yield return new WaitForFixedUpdate(); } } public void OnDestyoy_OBJ() { Destroy(this.gameObject); } }
2.倒计时的实现
创建一个Text 脚本挂在上面即可
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; /// <summary> /// 倒计时的实现 /// </summary> public class CountDownTime : MonoBehaviour { private Text text; int Shi; int Fen; int miao; private bool aa; public float time; // Start is called before the first frame update void Start() { text = this.GetComponent<Text>(); StartCoroutine("enumerator"); //Endkisijd(); } public void Endkisijd() { aa = true; } #region 第一种方法 IEnumerator enumerator() { while (time >= 0) { Shi = (int)time / 3600; Fen = (int)time % 3600 / 60; miao = (int)time % 60; text.text = string.Format ("{0}:{1}:{2}",Shi,Fen ,miao ); yield return new WaitForSeconds(1); time--; } } #endregion // Update is called once per frame void Update() { #region 第二种方法 /* if (aa) { Shi = (int)time / 3600; Fen = (int)time % 3600 / 60; miao = (int)time % 60; text.text = string.Format("{0}:{1}:{2}", Shi, Fen, miao); } if (time > 0) { time -= Time.deltaTime; } else { text.text = string.Format("{0}:{1}:{2}", 00, 00, 00); aa = false; } */ #endregion } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器