using System; using System.Collections.Generic; using System.Collections.Concurrent; using System.Linq; using System.Text; using System.Threading.Tasks; using UnityEngine; /// /// 轮询管理器 /// public class PollingManager : MonoSingleton { /// /// 轮询事务 /// public class PollItem { /// /// 唯一id /// public string Id; /// /// 回调函数 /// public Action Callback; /// /// 时间间隔(秒) /// public int Interval_sec; public void Update() { timespan += Time.deltaTime; if (timespan > Interval_sec) { timespan -= Interval_sec; Callback?.Invoke(); } } /// /// 时间 /// private float timespan = 0; } Dictionary PollItems = new Dictionary(); /// /// 注册轮询事务 /// /// 字符串:唯一id /// 时间间隔,单位:秒(若需更高精度请自行管理你的轮询) /// 回调函数 public void Register(string Uni_ID, int interval_seconds, Action callBack) { PollItems[Uni_ID] = new PollItem { Id = Uni_ID, Interval_sec = interval_seconds, Callback = callBack }; } /// /// 删除轮询事务 /// /// public void Unregister(string Uni_ID) { if (PollItems.ContainsKey(Uni_ID)) { PollItems.Remove(Uni_ID); } } private void FixedUpdate() { foreach (var item in PollItems.Values) { item.Update(); } } }