unity工具类篇 unity 计时器

一、计时器的主要功能

  1. 在规定时间内倒计时
  2. 显示倒计时时间
  3. 显示正计时时间
  4. 暂停、继续
  5. 时间速率影响
  6. 获取倒计时剩余时间
  7. 倒计时结束的回调

二、计时器实现

using UnityEngine;

public delegate void CompleteEvent();
public delegate void UpdateEvent(float t);

public class Timer : MonoBehaviour
{
    UpdateEvent updateEvent;
    CompleteEvent onCompleted;
    bool isLog = true;//是否打印消息
    float timeTarget;   // 计时时间/
    float timeStart;    // 开始计时时间/
    float offsetTime;   // 计时偏差/
    bool isTimer;       // 是否开始计时/
    bool isDestory = true;     // 计时结束后是否销毁/
    bool isEnd;         // 计时是否结束/
    bool isIgnoreTimeScale = true;  // 是否忽略时间速率
    bool isRepeate;     //是否重复
    float now;          //当前时间 正计时
    float downNow;          //倒计时
    bool isDownNow = false;     //是否是倒计时

    // 是否使用游戏的真实时间 不依赖游戏的时间速度
    float TimeNow
    {
        get { return isIgnoreTimeScale ? Time.realtimeSinceStartup : Time.time; }
    }

    /// 
    /// 创建计时器:名字  根据名字可以创建多个计时器对象
    /// 
    public static Timer createTimer(string gobjName = "Timer")
    {
        GameObject g = new GameObject(gobjName);
        Timer timer = g.AddComponent<Timer>();
        return timer;
    }

    /// 
    /// 开始计时
    /// 
    /// 目标时间
    /// 是否是倒计时
    /// 完成回调函数
    /// 计时器进程回调函数
    /// 是否忽略时间倍数
    /// 是否重复
    /// 完成后是否销毁
    public void startTiming(float timeTarget, bool isDownNow = false,
        CompleteEvent onCompleted_ = null, UpdateEvent update = null,
        bool isIgnoreTimeScale = true, bool isRepeate = false, bool isDestory = true,
        float offsetTime = 0, bool isEnd = false, bool isTimer = true)
    {
        this.timeTarget = timeTarget;
        this.isIgnoreTimeScale = isIgnoreTimeScale;
        this.isRepeate = isRepeate;
        this.isDestory = isDestory;
        this.offsetTime = offsetTime;
        this.isEnd = isEnd;
        this.isTimer = isTimer;
        this.isDownNow = isDownNow;
        timeStart = TimeNow;

        if (onCompleted_ != null)
            onCompleted = onCompleted_;
        if (update != null)
            updateEvent = update;
    }

    void Update()
    {
        if (isTimer)
        {
            now = TimeNow - offsetTime - timeStart;
            downNow = timeTarget - now;
            if (updateEvent != null)
            {
                if (isDownNow)
                {
                    updateEvent(downNow);
                }
                else
                {
                    updateEvent(now);
                }
            }
            if (now > timeTarget)
            {
                if (onCompleted != null)
                    onCompleted();
                if (!isRepeate)
                    destory();
                else
                    reStartTimer();
            }
        }
    }

    /// 
    /// 获取剩余时间
    /// 
    /// 
    public float GetTimeNow()
    {
        return Mathf.Clamp(timeTarget - now, 0, timeTarget);
    }

    /// 
    /// 计时结束
    /// 
    public void destory()
    {
        isTimer = false;
        isEnd = true;
        if (isDestory)
            Destroy(gameObject);
    }

    float _pauseTime;
    /// 
    /// 暂停计时
    /// 
    public void pauseTimer()
    {
        if (isEnd)
        {
            if (isLog) Debug.LogWarning("计时已经结束!");
        }
        else
        {
            if (isTimer)
            {
                isTimer = false;
                _pauseTime = TimeNow;
            }
        }
    }

    /// 
    /// 继续计时
    /// 
    public void connitueTimer()
    {
        if (isEnd)
        {
            if (isLog) Debug.LogWarning("计时已经结束!请从新计时!");
        }
        else
        {
            if (!isTimer)
            {
                offsetTime += (TimeNow - _pauseTime);
                isTimer = true;
            }
        }
    }

    /// 
    /// 重新计时
    /// 
    public void reStartTimer()
    {
        timeStart = TimeNow;
        offsetTime = 0;
    }

    /// 
    /// 更改目标时间
    /// 
    /// 
    public void changeTargetTime(float time_)
    {
        timeTarget = time_;
        timeStart = TimeNow;
    }


    /// 
    /// 游戏暂停调用
    /// 
    /// 
    void OnApplicationPause(bool isPause_)
    {
        if (isPause_)
        {
            pauseTimer();
        }
        else
        {
            connitueTimer();
        }
    }
}

三、格式化时间

 	/// 
    /// 格式化时间
    /// 
    /// 
    /// 
    public static string FormatTime(float seconds)
    {
        TimeSpan ts = new TimeSpan(0, 0, Convert.ToInt32(seconds));
        string str = "";
        if (ts.Hours > 0)
        {
            str = ts.Hours.ToString("00") + ":" + ts.Minutes.ToString("00") + ":" + ts.Seconds.ToString("00");
        }
        if (ts.Hours == 0 && ts.Minutes > 0)
        {
            str = ts.Minutes.ToString("00") + ":" + ts.Seconds.ToString("00");
        }
        if (ts.Hours == 0 && ts.Minutes == 0)
        {
            str = "00:" + ts.Seconds.ToString("00");
        }
        return str;
    }

参考链接:https://itmonon.blog.csdn.net/article/details/124827131?spm=1001.2014.3001.5502

你可能感兴趣的:(unity工具类篇,unity,游戏引擎,c#)