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, errorMessage = message }); } 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) { var task = tasks[i]; LogSystem.LogError($"Timer任务执行异常:\n" + $"错误信息: {task.errorMessage}\n" + $"异常类型: {e.GetType()}\n" + $"异常信息: {e.Message}\n" + $"调用堆栈: {e.StackTrace}\n" + $"目标对象: {(task.target != null ? task.target.GetType().Name : "null")}" ); } tasks.RemoveAt(i); // 执行完毕后移除 } } } }