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);
}
}