有之前的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();
}
--------------------------------------------------------------------------------------------------------------------------------------------