TimerManager.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace QFramework.TimeExtend
  6. {
  7. public class Timer
  8. {
  9. static List<Timer> timers = new List<Timer>();
  10. private Action<float> UpdateEvent;
  11. private System.Action EndEvent;
  12. /// <summary>
  13. /// 用户设定的定时时长
  14. /// </summary>
  15. private float _time = -1;
  16. /// <summary>
  17. /// 是否循环执行
  18. /// </summary>
  19. private bool _loop;
  20. /// <summary>
  21. /// 是否忽略Timescale
  22. /// </summary>
  23. private bool _ignorTimescale;
  24. /// <summary>
  25. /// 用户指定的定时器标志,便于手动清除、暂停、恢复
  26. /// </summary>
  27. private string _flag;
  28. public static TimerDriver driver = null;//拿驱动器的引用只是为了初始化驱动器
  29. /// <summary>
  30. /// 获得当前时间
  31. /// </summary>
  32. private float CurrentTime { get { return _ignorTimescale ? UnityEngine.Time.realtimeSinceStartup : UnityEngine.Time.time; } }
  33. /// <summary>
  34. /// 缓存时间
  35. /// </summary>
  36. private float cachedTime;
  37. /// <summary>
  38. /// 已经流逝的时光
  39. /// </summary>
  40. float timePassed;
  41. /// <summary>
  42. /// 计时器是否结束
  43. /// </summary>
  44. private bool _isFinish = false;
  45. /// <summary>
  46. /// 计时器是否暂停
  47. /// </summary>
  48. private bool _isPause = false;
  49. private static bool showLog = true;
  50. /// <summary>
  51. /// 确认是否输出Debug信息
  52. /// </summary>
  53. public static bool ShowLog { set { showLog = value; } }
  54. /// <summary>
  55. /// 当前定时器设定的时间
  56. /// </summary>
  57. public float Duration{ get { return _time; } }//
  58. /// <summary>
  59. /// 暂停计时器
  60. /// </summary>
  61. public bool IsPause
  62. {
  63. get { return _isPause; }
  64. set
  65. {
  66. if (value)
  67. {
  68. Pause();
  69. }
  70. else
  71. {
  72. Resum();
  73. }
  74. }
  75. }
  76. /// <summary>
  77. /// 构造定时器
  78. /// </summary>
  79. /// <param name="time">定时时长</param>
  80. /// <param name="flag">定时器标识符</param>
  81. /// <param name="loop">是否循环</param>
  82. /// <param name="ignorTimescale">是否忽略TimeScale</param>
  83. private Timer(float time, string flag, bool loop = false, bool ignorTimescale = true)
  84. {
  85. if (null == driver) driver = TimerDriver.Get; //初始化Time驱动
  86. _time = time;
  87. _loop = loop;
  88. _ignorTimescale = ignorTimescale;
  89. cachedTime = CurrentTime;
  90. if (timers.Exists((v) => { return v._flag == flag; }))
  91. {
  92. if (showLog) Debug.LogWarningFormat("【TimerTrigger(容错)】:存在相同的标识符【{0}】!", flag);
  93. }
  94. _flag = string.IsNullOrEmpty(flag) ? GetHashCode().ToString() : flag;//设置辨识标志符
  95. }
  96. /// <summary>
  97. /// 暂停计时
  98. /// </summary>
  99. private void Pause()
  100. {
  101. if (_isFinish)
  102. {
  103. if (showLog) Debug.LogWarning("【TimerTrigger(容错)】:计时已经结束!");
  104. }
  105. else
  106. {
  107. _isPause = true;
  108. }
  109. }
  110. /// <summary>
  111. /// 继续计时
  112. /// </summary>
  113. private void Resum()
  114. {
  115. if (_isFinish)
  116. {
  117. if (showLog) Debug.LogWarning("【TimerTrigger(容错)】:计时已经结束!");
  118. }
  119. else
  120. {
  121. if (_isPause)
  122. {
  123. cachedTime = CurrentTime - timePassed;
  124. _isPause = false;
  125. }
  126. else
  127. {
  128. if (showLog) Debug.LogWarning("【TimerTrigger(容错)】:计时并未处于暂停状态!");
  129. }
  130. }
  131. }
  132. /// <summary>
  133. /// 刷新定时器
  134. /// </summary>
  135. private void Update()
  136. {
  137. if (!_isFinish && !_isPause) //运行中
  138. {
  139. timePassed = CurrentTime - cachedTime;
  140. if (null != UpdateEvent) UpdateEvent.Invoke(Mathf.Clamp01(timePassed / _time));
  141. if (timePassed >= _time)
  142. {
  143. if (null != EndEvent) EndEvent.Invoke();
  144. if (_loop)
  145. {
  146. cachedTime = CurrentTime;
  147. }
  148. else
  149. {
  150. Stop();
  151. }
  152. }
  153. }
  154. }
  155. /// <summary>
  156. /// 回收定时器
  157. /// </summary>
  158. private void Stop()
  159. {
  160. if (timers.Contains(this))
  161. {
  162. timers.Remove(this);
  163. }
  164. _time = -1;
  165. _isFinish = true;
  166. _isPause = false;
  167. UpdateEvent = null;
  168. EndEvent = null;
  169. }
  170. #region--------------------------Static Function Extend-------------------------------------
  171. #region-------------AddEntity---------------
  172. /// <summary>
  173. /// 添加定时触发器
  174. /// </summary>
  175. /// <param name="time">定时时长</param>
  176. /// <param name="flag">定时器标识符</param>
  177. /// <param name="loop">是否循环</param>
  178. /// <param name="ignorTimescale">是否忽略TimeScale</param>
  179. /// <returns></returns>
  180. public static Timer AddTimer(float time, string flag = "", bool loop = false, bool ignorTimescale = true)
  181. {
  182. Timer timer = new Timer(time, flag, loop, ignorTimescale);
  183. timers.Add(timer);
  184. return timer;
  185. }
  186. #endregion
  187. #region-------------UpdateAllTimer---------------
  188. public static void UpdateAllTimer()
  189. {
  190. for (int i = 0; i < timers.Count; i++)
  191. {
  192. if (null != timers[i])
  193. {
  194. timers[i].Update();
  195. }
  196. }
  197. }
  198. #endregion
  199. #region-------------ValidateCheckTimer---------------
  200. /// <summary>
  201. /// 确认是否存在指定的定时器
  202. /// </summary>
  203. /// <param name="flag">标志位指定</param>
  204. public static bool Exist(string flag)
  205. {
  206. return timers.Exists((v) => { return v._flag == flag; });
  207. }
  208. /// <summary>
  209. /// 确认是否存在指定的定时器
  210. /// </summary>
  211. /// <param name="flag">定时器指定</param>
  212. public static bool Exist(Timer timer)
  213. {
  214. return timers.Contains(timer);
  215. }
  216. /// <summary>
  217. /// 获得指定的定时器
  218. /// </summary>
  219. /// <param name="flag">标志位指定</param>
  220. public static Timer GetTimer(string flag)
  221. {
  222. return timers.Find((v) => { return v._flag == flag; });
  223. }
  224. #endregion
  225. #region-------------Pause AND Resum Timer---------------
  226. /// <summary>
  227. /// 暂停用户指定的计时触发器
  228. /// </summary>
  229. /// <param name="flag">指定的标识符</param>
  230. public static void Pause(string flag)
  231. {
  232. Timer timer = GetTimer(flag);
  233. if (null != timer)
  234. {
  235. timer.Pause();
  236. }
  237. else
  238. {
  239. if (showLog) Debug.Log("【TimerTrigger(容错)】:定时器已完成触发或无此定时器!---Flag【" + flag + "】。");
  240. }
  241. }
  242. /// <summary>
  243. /// 暂停用户指定的计时触发器
  244. /// </summary>
  245. /// <param name="timer">指定的定时器</param>
  246. public static void Pause(Timer timer)
  247. {
  248. if (Exist(timer))
  249. {
  250. timer.Pause();
  251. }
  252. else
  253. {
  254. if (showLog) Debug.Log("【TimerTrigger(容错)】:此定时器已完成触发或无此定时器!");
  255. }
  256. }
  257. /// <summary>
  258. /// 恢复用户指定的计时触发器
  259. /// </summary>
  260. /// <param name="flag">指定的标识符</param>
  261. public static void Resum(string flag)
  262. {
  263. Timer timer = GetTimer(flag);
  264. if (null != timer)
  265. {
  266. timer.Resum();
  267. }
  268. else
  269. {
  270. if (showLog) Debug.Log("【TimerTrigger(容错)】:定时器已完成触发或无此定时器!---Flag【" + flag + "】。");
  271. }
  272. }
  273. /// <summary>
  274. /// 恢复用户指定的计时触发器
  275. /// </summary>
  276. /// <param name="timer">指定的定时器</param>
  277. public static void Resum(Timer timer)
  278. {
  279. if (Exist(timer))
  280. {
  281. timer.Resum();
  282. }
  283. else
  284. {
  285. if (showLog) Debug.Log("【TimerTrigger(容错)】:此定时器已完成触发或无此定时器!");
  286. }
  287. }
  288. #endregion
  289. #region-------------DelEntity---------------
  290. /// <summary>
  291. /// 删除用户指定的计时触发器
  292. /// </summary>
  293. /// <param name="flag">指定的标识符</param>
  294. public static void DelTimer(string flag)
  295. {
  296. Timer timer = GetTimer(flag);
  297. if (null != timer)
  298. {
  299. timer.Stop();
  300. }
  301. else
  302. {
  303. if (showLog) Debug.Log("【TimerTrigger(容错)】:此定时器已完成触发或无此定时器!");
  304. }
  305. }
  306. /// <summary>
  307. /// 删除用户指定的计时触发器
  308. /// </summary>
  309. /// <param name="flag">指定的定时器</param>
  310. public static void DelTimer(Timer timer)
  311. {
  312. if (Exist(timer))
  313. {
  314. timer.Stop();
  315. }
  316. else
  317. {
  318. if (showLog) Debug.Log("【TimerTrigger(容错)】:此定时器已完成触发或无此定时器!");
  319. }
  320. }
  321. /// <summary>
  322. /// 删除用户指定的计时触发器
  323. /// </summary>
  324. /// <param name="completedEvent">指定的完成事件(直接赋值匿名函数无效)</param>
  325. public static void DelTimer(System.Action completedEvent)
  326. {
  327. Timer timer = timers.Find((v) => { return v.EndEvent == completedEvent; });
  328. if (null != timer)
  329. {
  330. timer.Stop();
  331. }
  332. else
  333. {
  334. if (showLog) Debug.Log("【TimerTrigger(容错)】:定时器已完成触发或无此定时器!---方法名:【" + completedEvent.Method.Name + "】。");
  335. }
  336. }
  337. /// <summary>
  338. /// 删除用户指定的计时触发器
  339. /// </summary>
  340. /// <param name="updateEvent">指定的Update事件(直接赋值匿名函数无效)</param>
  341. public static void DelTimer(Action<float> updateEvent)
  342. {
  343. Timer timer = timers.Find((v) => { return v.UpdateEvent == updateEvent; });
  344. if (null != timer)
  345. {
  346. timer.Stop();
  347. }
  348. else
  349. {
  350. if (showLog) Debug.Log("【TimerTrigger(容错)】:定时器已完成触发或无此定时器!---方法名:【" + updateEvent.Method.Name + "】。");
  351. }
  352. }
  353. /// <summary>
  354. /// 删除运行中所有计时触发器
  355. /// </summary>
  356. public static void RemoveAll()
  357. {
  358. timers.ForEach((v) => { v.Stop(); });
  359. timers.Clear();
  360. }
  361. #endregion
  362. #endregion
  363. #region-------------AddEvent-------------------
  364. public void AddEvent(System.Action completedEvent)
  365. {
  366. if (null==EndEvent)
  367. {
  368. EndEvent = completedEvent;
  369. }
  370. else
  371. {
  372. Delegate[] delegates = EndEvent.GetInvocationList();
  373. if (!Array.Exists(delegates,(v)=> { return v ==(Delegate) completedEvent; }))
  374. {
  375. EndEvent += completedEvent;
  376. }
  377. }
  378. }
  379. public void AddEvent(Action<float> updateEvent)
  380. {
  381. if (null == UpdateEvent)
  382. {
  383. UpdateEvent = updateEvent;
  384. }
  385. else
  386. {
  387. Delegate[] delegates = UpdateEvent.GetInvocationList();
  388. if (!Array.Exists(delegates, (v) => { return v == (Delegate)updateEvent; }))
  389. {
  390. UpdateEvent += updateEvent;
  391. }
  392. }
  393. }
  394. #endregion
  395. #region ---------------运行中的定时器参数修改-----------
  396. /// <summary>
  397. /// 重新设置运行中的定时器的时间
  398. /// </summary>
  399. /// <param name="endTime">定时时长</param>
  400. public Timer SetTime(float endTime)
  401. {
  402. if (_isFinish)
  403. {
  404. if (showLog) Debug.LogWarning("【TimerTrigger(容错)】:计时已经结束!");
  405. }
  406. else
  407. {
  408. if (endTime == _time)
  409. {
  410. if (showLog) Debug.LogWarning("【TimerTrigger(容错)】:时间已被设置,请勿重复操作!");
  411. }
  412. else
  413. {
  414. if (endTime < 0)
  415. {
  416. if (showLog) Debug.Log("【TimerTrigger(容错)】:时间不支持负数,已自动取正!");
  417. endTime = Mathf.Abs(endTime);
  418. }
  419. if (endTime < timePassed)//如果用户设置时间已错失
  420. {
  421. if (showLog) Debug.LogFormat("【TimerTrigger(容错)】:时间设置过短【passed:set=>{0}:{1}】,事件提前触发!", timePassed, endTime);
  422. }
  423. _time = endTime;
  424. }
  425. }
  426. return this;
  427. }
  428. /// <summary>
  429. /// 设置运行中的定时器的loop状态
  430. /// </summary>
  431. /// <param name="loop"></param>
  432. public Timer Setloop(bool loop)
  433. {
  434. if (!_isFinish)
  435. {
  436. _loop = loop;
  437. }
  438. else
  439. {
  440. if (showLog) Debug.Log("【TimerTrigger(容错)】:定时器已失效,设置Loop Fail!");
  441. }
  442. return this;
  443. }
  444. /// <summary>
  445. /// 设置运行中的定时器的ignoreTimescale状态
  446. /// </summary>
  447. /// <param name="loop"></param>
  448. public Timer SetIgnoreTimeScale(bool ignoreTimescale)
  449. {
  450. if (!_isFinish)
  451. {
  452. _ignorTimescale = ignoreTimescale;
  453. }
  454. else
  455. {
  456. if (showLog) Debug.Log("【TimerTrigger(容错)】:定时器已失效,设置IgnoreTimescale Fail!");
  457. }
  458. return this;
  459. }
  460. #endregion
  461. }
  462. public class TimerDriver : MonoBehaviour
  463. {
  464. #region 单例
  465. private static TimerDriver _instance;
  466. public static TimerDriver Get
  467. {
  468. get
  469. {
  470. if (null == _instance)
  471. {
  472. _instance = FindObjectOfType<TimerDriver>() ?? new GameObject("TimerEntity").AddComponent<TimerDriver>();
  473. }
  474. return _instance;
  475. }
  476. private set { _instance = value; }
  477. }
  478. private void Awake()
  479. {
  480. _instance = this;
  481. }
  482. #endregion
  483. private void Update()
  484. {
  485. Timer.UpdateAllTimer();
  486. }
  487. }
  488. public static class TimerExtend
  489. {
  490. /// <summary>
  491. /// 当计时器计数完成时执行的事件链
  492. /// </summary>
  493. /// <param name="timer"></param>
  494. /// <param name="completedEvent"></param>
  495. /// <returns></returns>
  496. public static Timer OnCompleted(this Timer timer ,System.Action completedEvent)
  497. {
  498. if (null==timer)
  499. {
  500. return null;
  501. }
  502. timer.AddEvent(completedEvent);
  503. return timer;
  504. }
  505. /// <summary>
  506. /// 当计数器计时进行中执行的事件链
  507. /// </summary>
  508. /// <param name="timer"></param>
  509. /// <param name="updateEvent"></param>
  510. /// <returns></returns>
  511. public static Timer OnUpdated(this Timer timer, Action<float> updateEvent)
  512. {
  513. if (null == timer)
  514. {
  515. return null;
  516. }
  517. timer.AddEvent(updateEvent);
  518. return timer;
  519. }
  520. }
  521. }