Unity3d 简易计时器.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public delegate void CompleteEvent();
public delegate void UpdateEvent(float t);
public class HallTimer : MonoBehaviour
{
    UpdateEvent updateEvent;
    CompleteEvent completeEvent;

    /// <summary>
    /// 计时的总时间
    /// </summary>
    float timeTarget;
    /// <summary>
    /// 开始计时的时间
    /// </summary>
    float timeStart;
    /// <summary>
    /// 现在系统的时间
    /// </summary>
    float timeNow;
    /// <summary>
    /// 计时偏差
    /// </summary>
    float offsetTime;
    /// <summary>
    /// 是否已经开始计时
    /// </summary>
    bool isTimerStart;
    /// <summary>
    /// 是否在计时结束后自毁
    /// </summary>
    bool isKillSelf = true;
    /// <summary>
    /// 是否已经结束计时
    /// </summary>
    bool isEnd;
    /// <summary>
    /// 是否忽略时间速率
    /// </summary>
    bool isIgnoreTimeScale = true;
    /// <summary>
    /// 是否重复计时
    /// </summary>
    bool isRepeate;

    float _Time
    {
        get
        {
            return isIgnoreTimeScale ? Time.realtimeSinceStartup : Time.deltaTime;
        }
    }

    /// <summary>
    /// 现在的时间剩余
    /// </summary>
    float now;
    /// <summary>
    /// 暂停时的时间
    /// </summary>
    float _pauseTime;

    void Update()
    {
        if (isTimerStart)
        {
            timeNow = _Time - offsetTime;
            now = timeNow - timeStart;
            updateEvent?.Invoke(Mathf.Clamp01(now / timeTarget));
            if (now > timeTarget)
            {
                completeEvent?.Invoke();
                if (!isRepeate)
                {
                    destory();
                }
                else
                {
                    reStartTimer();
                }
            }
        }
    }

    public float GetLeftTime()
    {
        return Mathf.Clamp(timeTarget - now, 0, timeTarget);
    }

    /// <summary>
    /// 暂停计时
    /// </summary>
    public void pauseTimer()
    {
        if (isEnd)
        {
            if (Const.GAME_DEBUG == 1)
            {
                Debug.LogWarning("计时已经结束!");
            } 
        }
        else
        {
            if (isTimerStart)
            {
                isTimerStart = false;
                _pauseTime = _Time;
            }
        }
    }
    /// <summary>
    /// 继续计时
    /// </summary>
    public void connitueTimer()
    {
        if (isEnd)
        {
            if (Const.GAME_DEBUG == 1)
            {
                Debug.LogWarning("计时已经结束!请从新计时!");
            }
        }
        else
        {
            if (!isTimerStart)
            {
                offsetTime += (_Time - _pauseTime);
                isTimerStart = true;
            }
        }
    }

    private void OnApplicationPause(bool pause)
    {
        if (pause)
        {
            //pauseTimer();
        }
        else
        {
            //connitueTimer();       
        }
    }
    public void destory()
    {
        isTimerStart = false;
        isEnd = true;
        if (isKillSelf)
        {
            Destroy(gameObject);
        }
    }

    public void reStartTimer()
    {
        timeStart = _Time;
        offsetTime = 0;
    }

    /// <summary>开始计时</summary>
    /// <param name="time_">总时长</param>
    /// <param name="completeEvent_">计时结束回调</param>
    /// <param name="update">计时中回调</param>
    /// <param name="isDestory_">结束计时后是否自毁</param>
    /// <param name="isIgnoreTimeScale_">是否不受时间速率的影响</param>
    /// <param name="isRepeate_">是否重复计时</param>
    public void startTiming(float time_, CompleteEvent completeEvent_, UpdateEvent update = null, bool isIgnoreTimeScale_ = true, bool isRepeate_ = false, bool isDestory_ = true)
    {
        timeTarget = time_;
        if (completeEvent_ != null)
            completeEvent = completeEvent_;
        if (update != null)
            updateEvent = update;
        isKillSelf = isDestory_;
        isIgnoreTimeScale = isIgnoreTimeScale_;
        isRepeate = isRepeate_;

        timeStart = _Time;
        offsetTime = 0;
        isEnd = false;
        isTimerStart = true;
    }
    /// <summary>
    /// 创建计时器
    /// </summary>
    /// <param name="gobjName">
    /// 计时器名字
    /// </param>
    public static HallTimer createTimer(string gobjName = "HallTimer")
    {
        GameObject g = new GameObject(gobjName);
        HallTimer timer = g.AddComponent<HallTimer>();
        return timer;
    }
}

注释很清晰拉,引用自这里:
https://blog.csdn.net/fucun1984686003/article/details/46919775

留下评论

电子邮件地址不会被公开。 必填项已用*标注