41 lines
1.3 KiB
C#
41 lines
1.3 KiB
C#
using System.Collections.Generic;
|
|
using Common.Timer.Runtime.ScriptableObjects;
|
|
using JoeSiu.Common.Infrastructure.Runtime;
|
|
using TriInspector;
|
|
using UnityEngine;
|
|
|
|
namespace Common.Timer.Runtime
|
|
{
|
|
public class TimerManager : MonoSingleton<TimerManager>
|
|
{
|
|
[InfoBox("As ScriptableObject doesn't have Update() function, use this class to control the ScriptableObject timer's tick")]
|
|
[SerializeField]
|
|
private List<TimerBaseSO> _timerSOs = new();
|
|
public IReadOnlyList<TimerBaseSO> TimerSOs => _timerSOs.AsReadOnly();
|
|
|
|
protected override void Awake()
|
|
{
|
|
base.Awake();
|
|
DontDestroyOnLoad(this);
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
_timerSOs.ForEach(so => so?.Tick());
|
|
}
|
|
|
|
public void Add(TimerBaseSO timerBaseSO)
|
|
{
|
|
if (_timerSOs.Contains(timerBaseSO)) return;
|
|
_timerSOs.Add(timerBaseSO);
|
|
Debug.Log($"Added TimerBaseSO '{timerBaseSO.name}' to {nameof(TimerManager)}", this);
|
|
}
|
|
|
|
public void Remove(TimerBaseSO timerBaseSO)
|
|
{
|
|
if (!_timerSOs.Contains(timerBaseSO)) return;
|
|
_timerSOs.Remove(timerBaseSO);
|
|
Debug.Log( $"Removed TimerBaseSO '{timerBaseSO.name}' from {nameof(TimerManager)}", this);
|
|
}
|
|
}
|
|
} |