QQ扫一扫联系
UI不用继承MonoBehaviour,实现Update的TimerManager
/*---------------------------------------------------------------- Created by 王银 文件名: TimerManager 创建时间: 2022.6.16 文件功能描述:UI不用继承MonoBehaviour,实现Update的TimerManager {去除空行的正则表达式 (?<=\r\n)\r\n} Copyright © 2022年 王银 All rights reserved. ----------------------------------------------------------------*/ using UnityEngine; using System.Collections.Generic; public class TimerEntity { static int TimeCode = 1; public int code = 0; public float delay = 0; public float oldTime = 0; public int maxCount = 0; public TimerManager.TimerCallBack callBack; public bool isRemove = false; public TimerEntity(float delay, TimerManager.TimerCallBack callBack, int maxCount) { this.delay = delay; this.callBack = callBack; this.maxCount = maxCount; this.oldTime = Time.realtimeSinceStartup; this.code = TimerEntity.TimeCode++; } } public class TimerManager { /// <summary> /// 时钟回调 /// </summary> public delegate void TimerCallBack(int value); public static readonly TimerManager instance = new TimerManager(); private Dictionary<int, TimerEntity> timerDic; private List<TimerEntity> timerList; private int count = 0; public TimerManager() { timerDic = new Dictionary<int, TimerEntity>(); timerList = new List<TimerEntity>(); } public void Update() { if (count > 0) { TimerEntity timer; float nowTime = Time.realtimeSinceStartup; int num = 0; for (int i = 0; i < count;) { timer = timerList[i]; if (timer.isRemove) { RemoveTimer(timer, i); continue; } num = (int)((nowTime - timer.oldTime) / timer.delay); if (num > 0) { timer.maxCount -= num; timer.oldTime += num * timer.delay; try { timer.callBack(num); } catch (System.Exception e) { Debug.LogError("Stack=" + e.StackTrace); RemoveTimer(timer, i); continue; } if (timer.maxCount <= 0) { RemoveTimer(timer, i); continue; } } i++; } } } /// int id=TimerManager.instance.Add((int value1) => {}, 0.1f, 1, false); /// TimerManager.instance.Remove(id); /// <summary> /// 添加时钟 /// </summary> public int Add(TimerCallBack callBack, float delay, int maxCount = int.MaxValue, bool immediate = false) { TimerEntity timer = new TimerEntity(delay, callBack, maxCount); if (immediate) { try { callBack.Invoke(1); } catch (System.Exception e) { Debug.LogError("Stack=" + e.StackTrace); return -1; } } timerDic.Add(timer.code, timer); timerList.Add(timer); count++; return timer.code; } /// <summary> /// 移除时钟 /// </summary> public void Remove(int code) { TimerEntity timer; if (timerDic.TryGetValue(code, out timer)) { timer.isRemove = true; } } /// <summary> /// 移除时钟 /// </summary> private void RemoveTimer(TimerEntity timer, int index) { timer.callBack = null; timerDic.Remove(timer.code); timerList.RemoveAt(index); count--; } public void Clear() { timerDic.Clear(); timerList.Clear(); count = 0; } }
//使用 int id = TimerManager.instance.Add((int value1) => { //2秒后关闭Tip_UI UIManager.Instance.HideView(WindowName.Tip_UI); }, 2, 1); //本UI关闭时回收(或析构函数调用) TimerManager.instance.Remove(id);
在其他管理类中调用TimerManager的Update()即可!
Demo文件下载地址:
https://download.csdn.net/download/hack_yin/8569795