using System; using System.Collections.Generic; using Stary.Evo; using UnityEngine; namespace Stary.Evo { public interface ITimerSystem : ISystem { void CreateTimer(string timerName, float timeDuration, Action onArrivalTimeComplete); void RemoveTimer(string timerName); } public class TimerSystem : AbstractSystem, ITimerSystem { private static Dictionary _timerControllers = new Dictionary(); private static GameObject timerSystem; protected override void OnInit() { _timerControllers = new Dictionary(); timerSystem = new GameObject("Timer"); } public void CreateTimer(string timerName, float timeDuration, Action onArrivalTimeComplete) { if (_timerControllers.ContainsKey(timerName)) { Debug.LogError($"TimerSystem CreateTimer 重复创建定时器 {timerName}"); return; } var _timer = new GameObject(timerName); _timer.transform.SetParent(timerSystem.transform); var timerController = _timer.AddComponent(); _timerControllers.Add(timerName, timerController); timerController.StartTimer(timeDuration, onArrivalTimeComplete); } public void RemoveTimer(string timerName) { if (_timerControllers.ContainsKey(timerName)) { var timerController = _timerControllers[timerName]; GameObject.Destroy(timerController.gameObject); _timerControllers.Remove(timerName); } } public override void Dispose() { foreach (var controller in _timerControllers) { if (controller.Value != null) { GameObject.Destroy(controller.Value.gameObject); } } GameObject.Destroy(timerSystem.gameObject); } } }