Unity timer system

Very easy to use, convenient

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

public class TimerManager : MonoBehaviour
{
    private List<Timer> _usedTimes = new List<Timer>();
    private Queue<Timer> _canceledTimes = new Queue<Timer>();

    public void Update()
    {
        int count = _usedTimes.Count;
        for (int i = count -1; i >= 0; i--)
        {
            _usedTimes[i].Uptate();
        }
    }
    public void CancelTimer(Timer timer)
    {
        _usedTimes.Remove(timer);

        if(_canceledTimes.Count < 10)
        {
            timer.Cancel();
            _canceledTimes.Enqueue(timer);
        }
    }
    public Timer AddTimer(float delayTime, Action excuteFunc)
    {
        return AddTimer(delayTime, 1, delayTime, false, excuteFunc, null, null);
    }
    public Timer AddTimer(float duration, int loopTimes, Action excuteFunc)
    {
        return AddTimer(duration, loopTimes, duration, false, excuteFunc, null, null);
    }
    public Timer AddTimer(float duration, float loopTimes, float delayTime, Action excuteFunc)
    {
        return AddTimer(duration, loopTimes, delayTime, false, excuteFunc, null, null);
    }
    public Timer AddTimer(float duration, float loopTimes, a float  delayTime, BOOL isRealTime, the Action excuteFunc) 
    { 
        return AddTimer (DURATION, loopTimes, delayTime, isRealTime, excuteFunc, null , null ); 
    } 
    ///  <Summary> 
    ///  
    ///  </ Summary> 
    ///  <param name = "duration"> interval performed twice </ param> 
    ///  <param name = "loopTimes"> cycle times </ param> 
    ///  <param name = "delayTime"> first delay </ param> 
    ///  <param name = "isRealTime"> whether real time </ param> 
    ///  <param name = "excuteFunc "> executable </ param> 
    /// <param name="completeFunc">完成调用</param>
    /// <param name="finishFunc">取消时调用</param>
    /// <returns></returns>
    public Timer AddTimer(float duration, float loopTimes, float delayTime, bool isRealTime, Action excuteFunc, Action completeFunc, Action finishFunc)
    {
        Timer timer = null;
        if (_canceledTimes.Count > 0)
        {
            timer = _canceledTimes.Dequeue();
        }
        else
            timer = new Timer(this);

        timer.Create(duration, loopTimes, delayTime, isRealTime, excuteFunc, completeFunc, finishFunc);
        _usedTimes.Add(timer);

        return timer;
    }
}
using System;
using UnityEngine;

public class Timer
{
    public Timer(TimerManager tm)
    {
        timerManager = tm;
    }
    
    public float duration;
    public float loopTimes;
    public float delayTime;
    public bool isRealTime;
    public Action completeFunc;
    public Action finishFunc;
    public Action excuteFunc;

    private TimerManager timerManager;
    private float _lastDoneTime;
    private float _startTime;
    public void Create(float duration, float loopTimes, float delayTime, bool isRealTime, Action excuteFunc, Action completeFunc, Action finishFunc)
    {
        this.duration = duration;
        this.loopTimes = loopTimes;
        this.delayTime = delayTime;
        this.isRealTime = isRealTime;
        this.completeFunc = completeFunc;
        this.finishFunc = finishFunc;
        this.excuteFunc = excuteFunc;

        float curTime = GetTime();
        _startTime = curTime + delayTime;
    }
    public void Uptate()
    {
        float curTime = GetTime();
        if (_lastDoneTime < 0)
        {
            // 第一次执行
            if(curTime >= _startTime)
            {
                if (excuteFunc != null)
                    excuteFunc();

                _lastDoneTime = curTime;
                if(loopTimes != -1)
                {
                    loopTimes--;
                    if(loopTimes == 0)
                    {
                        if (finishFunc != null)
                            finishFunc();

                        timerManager.CancelTimer(this);
                    }
                }
            }
        }
        else
        {
            // 循环执行
            if (curTime - _lastDoneTime >= duration)
            {
                if(excuteFunc! = null ) 
                    excuteFunc (); 

                _lastDoneTime = curTime;
                if (loop times! = - 1 ) 
                { 
                    walking times - ;
                    if (loop times == 0 ) 
                    { 
                        if (finishFunc! = null ) 
                            finishFunc (); 

                        timerManager.CancelTimer ( this ); 
                    } 
                } 
            } 
        } 
    }
    private float GetTime()
    {
        if (isRealTime)
            return Time.realtimeSinceStartup;
        else
            return Time.time;
    }
    public void Cancel()
    {
        _lastDoneTime = -1;
        _startTime = 0;
    }
}

 

Guess you like

Origin www.cnblogs.com/sanyejun/p/12165768.html