From 0c51d17278fd74a46bc478b398e2370cd08e1d44 Mon Sep 17 00:00:00 2001 From: ALEXTANG <574809918@qq.com> Date: Mon, 8 Aug 2022 16:04:10 +0800 Subject: [PATCH] Update TimerMgr.cs --- Assets/TEngine/Runtime/Core/TimerMgr.cs | 41 ++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/Assets/TEngine/Runtime/Core/TimerMgr.cs b/Assets/TEngine/Runtime/Core/TimerMgr.cs index c7aa9e20..6ea145dc 100644 --- a/Assets/TEngine/Runtime/Core/TimerMgr.cs +++ b/Assets/TEngine/Runtime/Core/TimerMgr.cs @@ -1,9 +1,10 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using UnityEngine; namespace TEngine { - public class TimerMgr:UnitySingleton + public class TimerMgr : UnitySingleton { public delegate void TimerHandler(object[] args); @@ -13,9 +14,9 @@ namespace TEngine public float curTime = 0; public float time = 0; public TimerHandler handler; - public bool isLoop = false; + public bool isLoop = false; public bool isNeedRemove = false; - public bool isRunning = false; + public bool isRunning = false; public bool isUnscaled = false; //是否使用非缩放的时间 public object[] args = null; //回调参数 } @@ -413,5 +414,37 @@ namespace TEngine UpdateTimer(); UpdateUnscaledTimer(); } + + private List _ticker = new List(); + public System.Timers.Timer AddSystemTimer(Action callBack) + { + var timerTick = new System.Timers.Timer(); + int interval = 1000; + timerTick = new System.Timers.Timer(interval); + timerTick.AutoReset = true; + timerTick.Enabled = true; + timerTick.Elapsed += new System.Timers.ElapsedEventHandler(callBack); + + _ticker.Add(timerTick); + + return timerTick; + } + + protected override void OnDestroy() + { + DestroySystemTimer(); + base.OnDestroy(); + } + + private void DestroySystemTimer() + { + for (int i = 0; i < _ticker.Count; i++) + { + if (_ticker[i] != null) + { + _ticker[i].Stop(); + } + } + } } }