mirror of
https://github.com/ConjureETS/Unity_Utils.git
synced 2026-03-23 20:40:58 +00:00
50 lines
1.2 KiB
C#
50 lines
1.2 KiB
C#
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
|
|
namespace Core
|
|
{
|
|
public class Timer : MonoBehaviour
|
|
{
|
|
public UnityAction onTimeOver;
|
|
|
|
public bool IsRunning { get; private set; }
|
|
|
|
public float CurrentTime { get; private set; }
|
|
|
|
[SerializeField] private float _startingTime = 600;
|
|
[SerializeField] private bool _isCountDown;
|
|
|
|
private void Start()
|
|
{
|
|
CurrentTime = _startingTime;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (IsRunning)
|
|
{
|
|
if (_isCountDown)
|
|
{
|
|
if (CurrentTime > 0)
|
|
{
|
|
CurrentTime -= Time.deltaTime;
|
|
}
|
|
else
|
|
{
|
|
CurrentTime = 0;
|
|
IsRunning = false;
|
|
onTimeOver?.Invoke();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
CurrentTime += Time.deltaTime;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void StartTimer() => IsRunning = true;
|
|
public void StopTimer() => IsRunning = false;
|
|
}
|
|
}
|