增加安全定时器GameTimerTick

增加安全定时器GameTimerTick
This commit is contained in:
ALEXTANG
2023-11-28 14:25:25 +08:00
parent cb73c9a9eb
commit 6d376b0e07
2 changed files with 63 additions and 0 deletions

View File

@@ -0,0 +1,60 @@
using UnityEngine;
namespace TEngine
{
public class GameTimerTick
{
protected OnTick Handle;
protected float LastTime;
protected float Interval;
protected bool ResetInterval = true;
public GameTimerTick(float interval, OnTick tickHandle) => Init(interval, true, true, tickHandle);
public GameTimerTick(float interval, bool immediately, OnTick tickHandle) => Init(interval, immediately, true, tickHandle);
public GameTimerTick(float interval, bool immediately, bool resetInterval, OnTick tickHandle)
{
Init(interval, immediately, resetInterval, tickHandle);
}
private void Init(float interval, bool immediately, bool resetInterval, OnTick tickHandle)
{
Interval = interval;
Handle = tickHandle;
ResetInterval = resetInterval;
if (immediately)
{
return;
}
LastTime = Time.time;
}
public void OnUpdate()
{
float time = Time.time;
if (LastTime + Interval >= time)
{
return;
}
if (ResetInterval)
{
LastTime = time;
}
else if (LastTime == 0.0)
{
LastTime = time;
}
else
{
LastTime += Interval;
}
Handle();
}
public delegate void OnTick();
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 76843c14c1344aa68863289bf7388238
timeCreated: 1701088103