Files

31 lines
687 B
C#

namespace RedHotRoast
{
using System;
using UnityEngine;
public class Throttle
{
private float _lastTime;
private readonly float _interval;
public Throttle(float intervalSeconds)
{
_interval = intervalSeconds;
_lastTime = -intervalSeconds; // 确保一开始就能执行
}
/// <summary>
/// 节流调用:在时间窗口内只执行一次
/// </summary>
public void Execute(Action action)
{
if (Time.time - _lastTime >= _interval)
{
_lastTime = Time.time;
action?.Invoke();
}
}
}
}