写了一个计时器

由于工作需要写了个计时器,没有想像的那么容易,折腾了一个小时左右,终于大功告成。 主要就是把unity的计时转换成特定的格式而已,大的单位比如分,时,天不为0的时候只显示秒单位,其他在不为0的时候显示,代码很简单,就不注释了,截图也免了…没啥好看的,自己运行一下就清楚了哈哈

using System.Text;
using UnityEngine;
using UnityEngine.UI;
public class TimerTest : MonoBehaviour
{
    float day = 0;
    float hour = 0;
    float minute = 0;
    float second = 0;
    const int hex = 60;
    public Text showTime;
    void Start()
    {
        showTime.text = CutTimer(Time.time).ToString();
    }
    void Update()
    {
        showTime.text = CutTimer(Time.time).ToString();
    }
    StringBuilder sb = new StringBuilder();
    StringBuilder CutTimer(float t)
    {
        sb.Length = 0;
        second = t % hex;
        sb.Append(second.ToString("0.00"));
        sb.Append("秒");
        if (t >= hex)
        {
            minute = (t - second) / hex;
            sb.Insert(0, "分");
            sb.Insert(0, minute);
            if (minute >= hex)
            {
                float m = minute % hex;
                hour = (minute - m) / hex;
                minute = m;
                sb.Insert(0, "时");
                sb.Insert(0, hour);
                if (hour > 24)
                {
                    m = hour % 24;
                    day = (hour - m) / 24;
                    hour = m;
                    sb.Insert(0, "天");
                    sb.Insert(0, day);
                }
            }
        }
        return sb;
    }
    public void GamePause(bool state)//暂停游戏
    {
        if (state)//暂停游戏
        {
            Time.timeScale = 0;
        }
        else//恢复 
        {
            Time.timeScale = 1;
        }
    }
}

刚刚测试了下,发现还是有bug,在到达小时单位时,时间进位之后小的那个单位应该是要去掉进位的那部分才对,修正后代码如下:

 float day = 0;
    float hour = 0;
    float minute = 0;
    float second = 0;
    const int hex = 60;
    public Text showTime;
    void Start()
    {
        showTime.text = CutTimer(Time.time).ToString();
    }
    void Update()
    {
        showTime.text = CutTimer(Time.time).ToString();
    }
    StringBuilder sb = new StringBuilder();
    StringBuilder CutTimer(float t)
    {
        sb.Length = 0;
        second = t % hex;
        sb.Append(second.ToString("0.00"));
        sb.Append("秒");
        if (t >= hex)
        {
            minute = (t - second) / hex;
            if (minute >= hex)
            {
                float m = minute % hex;
                hour = (minute - m) / hex;
                minute = m;
                sb.Insert(0, "分");
                sb.Insert(0, minute);
                if (hour > 24)
                {
                    m = hour % 24;
                    day = (hour - m) / 24;
                    hour = m;
                    sb.Insert(0, "时");
                    sb.Insert(0, hour);
                    sb.Insert(0, "天");
                    sb.Insert(0, day);
                }
                else
                {
                    sb.Insert(0, "时");
                    sb.Insert(0, hour);
                }
            }
            else
            {
                sb.Insert(0, "分");
                sb.Insert(0, minute);
            }
        }
        return sb;
    }
    public void GamePause(bool state)//暂停游戏
    {
        if (state)//暂停游戏
        {
            Time.timeScale = 0;
        }
        else//恢复 
        {
            Time.timeScale = 1;
        }
    }

就测试了到了小时,至于天。。。出了bug找我!

你可能感兴趣的:(Unity3D_原创)