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
    }
}
复制代码

 

posted @   剑起苍穹  阅读(1893)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
/*鼠标点击特效*/
点击右上角即可分享
微信分享提示