Files
plugin-library/Assets/00.StaryEvo/Runtime/Tool/Timer/TimerSystem.cs

67 lines
2.1 KiB
C#
Raw Normal View History

2026-05-12 13:15:50 +08:00
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<string, TimerController>
_timerControllers = new Dictionary<string, TimerController>();
private static GameObject timerSystem;
protected override void OnInit()
{
_timerControllers = new Dictionary<string, TimerController>();
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<TimerController>();
_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);
}
}
}