201710030245->TimerMgr中对所有Timer进行管理

有之前的Timer基础类之后

只要在manager中写入构造函数,赋予timerid号,缓存数据

如果timer已经结束,则调用destory,清理数据

TimerMgr中方法:

1.dictionaray<int,Timer> 缓存所有Timer

2.Subscrics提供Timer构造数据

3.Destrory回收缓存数据

--------------------------------------------------------------------------------------------------------------------------------------------

详细代码:

public class TimerMgr : MonoSingleton<TimerMgr>
{
    private Dictionary<int, Timer> timers;
    private List<int> removeList;
    private int timerId = 0;
    public override void OnInitialized()
    {
        base.OnInitialized();
        timers = new Dictionary<int, Timer>();
        removeList = new List<int>();
    }
    public Timer Sucribe(float duringTime, TimerCallBack onStartCallBack = null)
    {
        timerId++;
        Timer t = new Timer(Instance, timerId, duringTime, onStartCallBack);
        timers.Add(timerId, t);
        return timers[timerId];
    }
    public void Update()
    {
        if (!timers.IsNullOrEmpty())
        {
            foreach (var item in timers.Values)
            {
                item.OnUpdate();
            }
        }
    }
    public void LateUpdate()
    {
        if (removeList.IsNonNullOrEmpty())
        {
            for (int i = 0; i < removeList.Count; i++)
            {
                timers[removeList[i]] = null;
                timers.Remove(removeList[i]);
            }
            removeList.Clear();
        }
    }
    public void DestoryObject(int id)
    {
        removeList.Add(id);
    }
    public override void OnDestory()
    {
        base.OnDestory();
        timers = null;
        removeList = null;
        timerId = 0;
    }   

    public delegate void TimerCallBack();
}

--------------------------------------------------------------------------------------------------------------------------------------------


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值