Unity_Utils/Common/TimerWrapper.cs

44 lines
1.1 KiB
C#

using UnityEngine;
using UnityEngine.Events;
namespace Common
{
public class TimerWrapper : MonoBehaviour
{
public UnityAction OnTimerOver;
[SerializeField, Min(0)] private float startTime;
[SerializeField] private bool countDown = true;
[SerializeField] private bool startOnAwake;
[SerializeField] private bool startOnStart;
private Timer timer;
public bool IsRunning => timer.IsRunning;
public float CurrentTime => timer.CurrentTime;
private void Awake()
{
timer = new Timer(startTime, countDown);
timer.OnTimeOver += OnTimeOver;
if (startOnAwake) timer.Start();
}
private void Start()
{
if (startOnStart) timer.Start();
}
private void Update()
{
timer.Tick(Time.deltaTime);
}
public void StartTimer() => timer.Start();
public void PauseTimer() => timer.Pause();
public void StopTimer() => timer.Stop();
public void ResetTimer() => timer.Reset();
private void OnTimeOver() => OnTimerOver?.Invoke();
}
}