using System; using System.Collections.Generic; using Logic.CrashSight; using Unity.VisualScripting; using UnityEngine; public class Timer { public static Timer Instance { get; private set; } private List tasks = new List(); // 存储所有定时任务 private class TimerTask { public object target; // 目标对象 A public Action method; // 方法 B public float executeTime; // 任务执行时间(Time.time + C 毫秒) public string errorMessage; } public Timer() { Instance = this; } // 注册定时任务 public void TimerRegister(object A, Action B, float C,string message) { tasks.Add(new TimerTask { target = A, method = B, executeTime = Time.time + C }); } public void Update() { if (tasks.Count == 0) return; float currentTime = Time.time; for (int i = tasks.Count - 1; i >= 0; i--) { if (currentTime >= tasks[i].executeTime) { try { tasks[i].method?.Invoke(); } catch (Exception e) { LogSystem.LogError("TimerErrorMessage : " + tasks[i].errorMessage); } tasks.RemoveAt(i); // 执行完毕后移除 } } } }