2025-01-23 19:06:48 +08:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using AlicizaX.Runtime;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace AlicizaX.Timer.Runtime
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 计时器组件。
|
|
|
|
|
/// </summary>
|
|
|
|
|
[DisallowMultipleComponent]
|
|
|
|
|
[AddComponentMenu("Game Framework/Timer")]
|
|
|
|
|
[UnityEngine.Scripting.Preserve]
|
|
|
|
|
public class TimerComponent : GameFrameworkComponent
|
|
|
|
|
{
|
|
|
|
|
ITimerManager _timerManager;
|
|
|
|
|
|
|
|
|
|
protected override void Awake()
|
|
|
|
|
{
|
|
|
|
|
ImplementationComponentType = Utility.Assembly.GetType(componentType);
|
|
|
|
|
InterfaceComponentType = typeof(ITimerManager);
|
|
|
|
|
base.Awake();
|
|
|
|
|
_timerManager = SysModuleCenter.GetModule<ITimerManager>();
|
|
|
|
|
if (_timerManager == null)
|
|
|
|
|
{
|
|
|
|
|
Log.Fatal("Timer manager is invalid.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-01-24 16:21:00 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 添加计时器。
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="callback">计时器回调。</param>
|
|
|
|
|
/// <param name="time">计时器间隔。</param>
|
|
|
|
|
/// <param name="isLoop">是否循环。</param>
|
|
|
|
|
/// <param name="isUnscaled">是否不收时间缩放影响。</param>
|
|
|
|
|
/// <param name="args">传参。(避免闭包)</param>
|
|
|
|
|
/// <returns>计时器Id。</returns>
|
|
|
|
|
public int AddTimer(TimerHandler callback, float time, bool isLoop = false, bool isUnscaled = false, params object[] args)
|
|
|
|
|
{
|
|
|
|
|
if (_timerManager == null)
|
|
|
|
|
{
|
|
|
|
|
throw new GameFrameworkException("TimerMgr is invalid.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _timerManager.AddTimer(callback, time, isLoop, isUnscaled, args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 暂停计时器。
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="timerId">计时器Id。</param>
|
|
|
|
|
public void Stop(int timerId)
|
|
|
|
|
{
|
|
|
|
|
if (_timerManager == null)
|
|
|
|
|
{
|
|
|
|
|
throw new GameFrameworkException("TimerMgr is invalid.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_timerManager.Stop(timerId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 恢复计时器。
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="timerId">计时器Id。</param>
|
|
|
|
|
public void Resume(int timerId)
|
|
|
|
|
{
|
|
|
|
|
if (_timerManager == null)
|
|
|
|
|
{
|
|
|
|
|
throw new GameFrameworkException("TimerMgr is invalid.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_timerManager.Resume(timerId);
|
|
|
|
|
}
|
2025-01-23 19:06:48 +08:00
|
|
|
|
|
|
|
|
/// <summary>
|
2025-01-24 16:21:00 +08:00
|
|
|
/// 计时器是否在运行中。
|
2025-01-23 19:06:48 +08:00
|
|
|
/// </summary>
|
2025-01-24 16:21:00 +08:00
|
|
|
/// <param name="timerId">计时器Id。</param>
|
|
|
|
|
/// <returns>否在运行中。</returns>
|
|
|
|
|
public bool IsRunning(int timerId)
|
2025-01-23 19:06:48 +08:00
|
|
|
{
|
2025-01-24 16:21:00 +08:00
|
|
|
if (_timerManager == null)
|
|
|
|
|
{
|
|
|
|
|
throw new GameFrameworkException("TimerMgr is invalid.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _timerManager.IsRunning(timerId);
|
2025-01-23 19:06:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-01-24 16:21:00 +08:00
|
|
|
/// 获得计时器剩余时间。
|
2025-01-23 19:06:48 +08:00
|
|
|
/// </summary>
|
2025-01-24 16:21:00 +08:00
|
|
|
public float GetLeftTime(int timerId)
|
2025-01-23 19:06:48 +08:00
|
|
|
{
|
2025-01-24 16:21:00 +08:00
|
|
|
if (_timerManager == null)
|
|
|
|
|
{
|
|
|
|
|
throw new GameFrameworkException("TimerMgr is invalid.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _timerManager.GetLeftTime(timerId);
|
2025-01-23 19:06:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-01-24 16:21:00 +08:00
|
|
|
/// 重置计时器,恢复到开始状态。
|
2025-01-23 19:06:48 +08:00
|
|
|
/// </summary>
|
2025-01-24 16:21:00 +08:00
|
|
|
public void Restart(int timerId)
|
2025-01-23 19:06:48 +08:00
|
|
|
{
|
2025-01-24 16:21:00 +08:00
|
|
|
if (_timerManager == null)
|
|
|
|
|
{
|
|
|
|
|
throw new GameFrameworkException("TimerMgr is invalid.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_timerManager.Restart(timerId);
|
2025-01-23 19:06:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-01-24 16:21:00 +08:00
|
|
|
/// 重置计时器。
|
2025-01-23 19:06:48 +08:00
|
|
|
/// </summary>
|
2025-01-24 16:21:00 +08:00
|
|
|
public void Reset(int timerId, TimerHandler callback, float time, bool isLoop = false, bool isUnscaled = false)
|
2025-01-23 19:06:48 +08:00
|
|
|
{
|
2025-01-24 16:21:00 +08:00
|
|
|
if (_timerManager == null)
|
|
|
|
|
{
|
|
|
|
|
throw new GameFrameworkException("TimerMgr is invalid.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_timerManager.Reset(timerId, callback, time, isLoop, isUnscaled);
|
2025-01-23 19:06:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-01-24 16:21:00 +08:00
|
|
|
/// 重置计时器。
|
2025-01-23 19:06:48 +08:00
|
|
|
/// </summary>
|
2025-01-24 16:21:00 +08:00
|
|
|
public void Reset(int timerId, float time, bool isLoop, bool isUnscaled)
|
2025-01-23 19:06:48 +08:00
|
|
|
{
|
2025-01-24 16:21:00 +08:00
|
|
|
if (_timerManager == null)
|
|
|
|
|
{
|
|
|
|
|
throw new GameFrameworkException("TimerMgr is invalid.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_timerManager.Reset(timerId, time, isLoop, isUnscaled);
|
2025-01-23 19:06:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-01-24 16:21:00 +08:00
|
|
|
/// 移除计时器。
|
2025-01-23 19:06:48 +08:00
|
|
|
/// </summary>
|
2025-01-24 16:21:00 +08:00
|
|
|
/// <param name="timerId">计时器Id。</param>
|
|
|
|
|
public void RemoveTimer(int timerId)
|
2025-01-23 19:06:48 +08:00
|
|
|
{
|
2025-01-24 16:21:00 +08:00
|
|
|
if (_timerManager == null)
|
|
|
|
|
{
|
|
|
|
|
Log.Error("TimerMgr is invalid.");
|
|
|
|
|
throw new GameFrameworkException("TimerMgr is invalid.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_timerManager.RemoveTimer(timerId);
|
2025-01-23 19:06:48 +08:00
|
|
|
}
|
|
|
|
|
|
2025-01-24 16:21:00 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 移除所有计时器。
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void RemoveAllTimer()
|
2025-01-23 19:06:48 +08:00
|
|
|
{
|
2025-01-24 16:21:00 +08:00
|
|
|
if (_timerManager == null)
|
|
|
|
|
{
|
|
|
|
|
Log.Error("TimerMgr is invalid.");
|
|
|
|
|
throw new GameFrameworkException("TimerMgr is invalid.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_timerManager.RemoveAllTimer();
|
2025-01-23 19:06:48 +08:00
|
|
|
}
|
|
|
|
|
}
|
2025-01-24 16:21:00 +08:00
|
|
|
}
|