计时器加(延时委托)

来源:互联网 发布:.io域名 编辑:程序博客网 时间:2024/05/21 15:05
using UnityEngine;using System.Collections.Generic;public class TimerManager : Singleton<TimerManager> {    //计时器表    private List<Timer> _list = new List<Timer>();    //添加计时器--单次    public Timer Add(float _delayTime,DelayDelegate _function)    {        Timer timer = new Timer(_delayTime, _function);        _list.Add(timer);        return timer;    }    //添加计时器--多次    public Timer Add(float _delayTime, DelayDelegate _function, int _targetInvokeTime)    {        Timer timer = new Timer(_delayTime, _function, _targetInvokeTime);        _list.Add(timer);        return timer;    }//添加计时器--循环    public void Add(float _delayTime, DelayDelegate _function, bool _isLoop)    {        Timer timer = new Timer(_delayTime, _function, _isLoop);        _list.Add(timer);    }    //移除计时器    public void Remove(Timer timer)    {        _list.Remove(timer);    }    //计时器更新    public void Update(float dt)    {        for (int i = 0; i < _list.Count; i++)        {            Timer timer = _list[i];            timer.Update(dt);            if (timer.IsDone)            {                _list.Remove(timer);            }        }    }    //获取一个计时器    public Timer Get(Timer timer)    {        if (_list.Contains(timer))        {            return timer;        }        return null;    }    //清除    public void Clear()    {        _list.Clear();    }}  //延时委托public delegate void DelayDelegate();/// <summary>/// 计时类/// </summary>public class Timer{    private float _delayTime;    private DelayDelegate _function;    private float _usedTime;    private bool _isDone = false;    public bool IsDone { get { return _isDone; } }    private int _targetInvokeTime;    private int _useInvokeTime;    private bool _isLoop = false;    public Timer(float _delayTime, DelayDelegate _function)    {        this._delayTime = _delayTime;        this._function = _function;    }    /// <summary>    /// 多次延迟调用    /// </summary>    /// <param name="_delayTime"></param>    /// <param name="_function"></param>    /// <param name="_targetInvokeTime"></param>    public Timer(float _delayTime, DelayDelegate _function, int _targetInvokeTime)    {        this._delayTime = _delayTime;        this._function = _function;        this._targetInvokeTime = _targetInvokeTime;    }    /// <summary>    /// 循环调用    /// </summary>    /// <param name="_delayTime"></param>    /// <param name="_function"></param>    public Timer(float _delayTime, DelayDelegate _function, bool _isLoop)    {        this._delayTime = _delayTime;        this._function = _function;        this._isLoop = _isLoop;    }    public void Update(float dt)    {        _usedTime += dt;        if (_usedTime >= _delayTime && _isDone == false)        {            _function();            if (_isLoop == false)            {                _useInvokeTime++;                if (_targetInvokeTime == 0 || _useInvokeTime == _targetInvokeTime)                {                    _isDone = true;                }            }            _usedTime = _usedTime - _delayTime;        }    }}
原创粉丝点击