91 lines
1.7 KiB
C#
91 lines
1.7 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
public class TimerCounter : MonoBehaviour
|
|
{
|
|
public bool IsRunning => mIsRunning;
|
|
|
|
private Action mDelDone;
|
|
|
|
private bool mIsRunning = false;
|
|
|
|
private float mTotalTime;
|
|
private float mTimeCounter = -1;
|
|
private int mCount = 0;
|
|
|
|
private bool mIgnoreTimeScale = false;
|
|
|
|
public void Update()
|
|
{
|
|
if (mIsRunning)
|
|
{
|
|
if (mTimeCounter > 0)
|
|
{
|
|
mTimeCounter -= mIgnoreTimeScale ? Time.unscaledDeltaTime : Time.deltaTime;
|
|
if (mTimeCounter <= 0)
|
|
{
|
|
Done();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void StartTimer(Action pDelDone, float pSeconds, int pCount = 1, bool pIgnoreTimeScale = false)
|
|
{
|
|
mDelDone = pDelDone;
|
|
|
|
mTotalTime = pSeconds;
|
|
mCount = pCount;
|
|
mIgnoreTimeScale = pIgnoreTimeScale;
|
|
|
|
if (pSeconds <= 0)
|
|
{
|
|
Done();
|
|
}
|
|
else
|
|
{
|
|
ExecuteOnce();
|
|
}
|
|
}
|
|
|
|
public void ResetTimeDelta(float pSeconds)
|
|
{
|
|
mTotalTime = pSeconds;
|
|
}
|
|
|
|
public void CancelTimer()
|
|
{
|
|
mIsRunning = false;
|
|
mTotalTime = 0;
|
|
mTimeCounter = -1;
|
|
|
|
mDelDone = null;
|
|
}
|
|
|
|
public void PauseTimer(bool pPause)
|
|
{
|
|
mIsRunning = !pPause;
|
|
}
|
|
|
|
private void ExecuteOnce()
|
|
{
|
|
mIsRunning = true;
|
|
mTimeCounter = mTotalTime;
|
|
}
|
|
|
|
private void Done()
|
|
{
|
|
mIsRunning = false;
|
|
mDelDone?.Invoke();
|
|
|
|
mCount--;
|
|
if (mCount > 0)
|
|
{
|
|
ExecuteOnce();
|
|
}
|
|
else
|
|
{
|
|
mTimeCounter = 0;
|
|
}
|
|
}
|
|
} |