通用计时器功能

unity 很多时候 用update当计时器,我这里实现一个不用update实现的

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Logic.Base
{
    public class Timer
    {
        // Fields
        private DateTime _expiredTime;
        private TimerHeap.Handler _handler;
        private int _intervals;
        private object _param;
        public long key;
        // Methods
        public Timer(int intervals, TimerHeap.Handler handler, object param, bool repeated, long _key)
        {
            this._intervals = intervals;
            this._expiredTime = DateTime.Now;
            this._handler = handler;
            this._param = param;
            this.Repeated = repeated;
            this.Destoryed = false;
            this.key = _key;
        }


        public void AddIntervals()
        {
            TimeSpan span = new TimeSpan(0, 0, 0, 0, this._intervals);
            this._expiredTime += span;
        }


        public bool Expired()
        {
            TimeSpan span = (TimeSpan)(DateTime.Now - this._expiredTime);
            if (span.Ticks < 0L)
            {
                return false;
            }
            try
            {
                if (this._handler != null)
                {
                    this._handler(this._param);
                }
            }
            catch (Exception exception)
            {
                Debug.LogError(exception.StackTrace);
            }
            return true;
        }


        public long Ticks()
        {
            return this._expiredTime.Ticks;
        }


        // Properties
        public bool Destoryed { get; set; }


        public bool Repeated { get; private set; }
    }

}


using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Logic.Base;
namespace Logic.Base

{

//这是一个单例,主要用于创建计时器

    public class TimerHeap : BaseSingle<TimerHeap>
    {
        // Fields
        private List<Timer> _addingTimers = new List<Timer>();
        private List<Timer> _deletingTimers = new List<Timer>();
        private SortedDictionary<long, List<Timer>> _timerHeap = new SortedDictionary<long, List<Timer>>();
        private static long key = 0;


        // 创建计时器
        public Timer CreateTimer(int intervals, Handler handler, object param = null, bool repeated = false)
        {
            Timer item = new Timer(intervals, handler, param, repeated, key);
            key++;
            this._addingTimers.Add(item);
            return item;

        }

//删除计时器

        public void DeleteTimer(Timer timer)
        {
            if (!_deletingTimers.Contains(timer))
                _deletingTimers.Add(timer);
        }
        private void DoCreateTimer(Timer timer)
        {
            List<Timer> list;
            long key = timer.key;
            if (!this._timerHeap.TryGetValue(key, out list))
            {
                list = new List<Timer>();
                this._timerHeap.Add(key, list);
            }
            list.Add(timer);
        }


        private void DoDestoryTimer(Timer timer)
        {
            List<Timer> list;
            if (this._timerHeap.TryGetValue(timer.key, out list))
            {
                list.Remove(timer);
                if (list.Count == 0)
                {
                    this._timerHeap.Remove(timer.key);
                }
            }
        }

//这个函数要放到uodate里运行
        public void Update()
        {
            for (int i = 0; i < this._deletingTimers.Count; i++)
            {
                this.DoDestoryTimer(this._deletingTimers[i]);
            }
            for (int j = 0; j < this._addingTimers.Count; j++)
            {
                Timer timer = this._addingTimers[j];
                if (!timer.Destoryed)
                {
                    timer.AddIntervals();
                    this.DoCreateTimer(timer);
                }
            }
            this._addingTimers.Clear();
            this._deletingTimers.Clear();
            foreach (List<Timer> list in this._timerHeap.Values)
            {
                bool flag4 = true;
                for (int k = 0; k < list.Count; k++)
                {
                    Timer item = list[k];
                    if (!item.Expired())
                    {
                        flag4 = false;
                        break;
                    }
                    this._deletingTimers.Add(item);
                    if (item.Repeated)
                    {
                        this._addingTimers.Add(item);
                    }
                }
                if (!flag4)
                {
                    break;
                }
            }
        }


        // Nested Types
        public delegate void Handler(object param);
    }
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值