增加计时器TimerModule。

增加计时器TimerModule。
This commit is contained in:
ALEXTANG
2023-11-22 12:20:08 +08:00
parent ff613e4130
commit bd0cfc5577
7 changed files with 707 additions and 10 deletions

View File

@@ -153,7 +153,7 @@ Transform:
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
m_Children: [] m_Children: []
m_Father: {fileID: 709048975} m_Father: {fileID: 709048975}
m_RootOrder: 10 m_RootOrder: 11
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &450845954 --- !u!114 &450845954
MonoBehaviour: MonoBehaviour:
@@ -381,7 +381,7 @@ Transform:
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
m_Children: [] m_Children: []
m_Father: {fileID: 709048975} m_Father: {fileID: 709048975}
m_RootOrder: 6 m_RootOrder: 7
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &683825228 --- !u!114 &683825228
MonoBehaviour: MonoBehaviour:
@@ -441,6 +441,7 @@ Transform:
m_Children: m_Children:
- {fileID: 964133197} - {fileID: 964133197}
- {fileID: 1751957488} - {fileID: 1751957488}
- {fileID: 1029867562}
- {fileID: 877336934} - {fileID: 877336934}
- {fileID: 803382966} - {fileID: 803382966}
- {fileID: 790894654} - {fileID: 790894654}
@@ -505,7 +506,7 @@ Transform:
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
m_Children: [] m_Children: []
m_Father: {fileID: 709048975} m_Father: {fileID: 709048975}
m_RootOrder: 4 m_RootOrder: 5
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &790894655 --- !u!114 &790894655
MonoBehaviour: MonoBehaviour:
@@ -571,7 +572,7 @@ Transform:
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
m_Children: [] m_Children: []
m_Father: {fileID: 709048975} m_Father: {fileID: 709048975}
m_RootOrder: 3 m_RootOrder: 4
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &803382967 --- !u!114 &803382967
MonoBehaviour: MonoBehaviour:
@@ -617,7 +618,7 @@ Transform:
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
m_Children: [] m_Children: []
m_Father: {fileID: 709048975} m_Father: {fileID: 709048975}
m_RootOrder: 2 m_RootOrder: 3
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &877336935 --- !u!114 &877336935
MonoBehaviour: MonoBehaviour:
@@ -696,7 +697,7 @@ Transform:
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
m_Children: [] m_Children: []
m_Father: {fileID: 709048975} m_Father: {fileID: 709048975}
m_RootOrder: 9 m_RootOrder: 10
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &914171639 --- !u!114 &914171639
MonoBehaviour: MonoBehaviour:
@@ -790,7 +791,7 @@ Transform:
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
m_Children: [] m_Children: []
m_Father: {fileID: 709048975} m_Father: {fileID: 709048975}
m_RootOrder: 5 m_RootOrder: 6
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &965975542 --- !u!114 &965975542
MonoBehaviour: MonoBehaviour:
@@ -814,6 +815,50 @@ MonoBehaviour:
downloadingMaxNum: 3 downloadingMaxNum: 3
failedTryAgain: 3 failedTryAgain: 3
adaptiveReplacementCacheCapacity: 32 adaptiveReplacementCacheCapacity: 32
--- !u!1 &1029867561
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1029867562}
- component: {fileID: 1029867563}
m_Layer: 0
m_Name: Timer
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &1029867562
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1029867561}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 709048975}
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1029867563
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1029867561}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 5e79c115d5054209810f42dc6e25cf94, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &1047779123 --- !u!1 &1047779123
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@@ -844,7 +889,7 @@ Transform:
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
m_Children: [] m_Children: []
m_Father: {fileID: 709048975} m_Father: {fileID: 709048975}
m_RootOrder: 11 m_RootOrder: 12
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1047779125 --- !u!114 &1047779125
MonoBehaviour: MonoBehaviour:
@@ -967,7 +1012,7 @@ Transform:
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
m_Children: [] m_Children: []
m_Father: {fileID: 709048975} m_Father: {fileID: 709048975}
m_RootOrder: 7 m_RootOrder: 8
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1666908678 --- !u!114 &1666908678
MonoBehaviour: MonoBehaviour:
@@ -1055,7 +1100,7 @@ Transform:
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
m_Children: [] m_Children: []
m_Father: {fileID: 709048975} m_Father: {fileID: 709048975}
m_RootOrder: 8 m_RootOrder: 9
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1863338242 --- !u!114 &1863338242
MonoBehaviour: MonoBehaviour:

View File

@@ -99,6 +99,13 @@ namespace TEngine
public static SceneModule Scene => _scene ??= Get<SceneModule>(); public static SceneModule Scene => _scene ??= Get<SceneModule>();
private static SceneModule _scene; private static SceneModule _scene;
/// <summary>
/// 获取计时器模块。
/// </summary>
public static TimerModule Timer => _timer ??= Get<TimerModule>();
private static TimerModule _timer;
#endregion #endregion
/// <summary> /// <summary>
@@ -153,6 +160,7 @@ namespace TEngine
_ui = null; _ui = null;
_localization = null; _localization = null;
_scene = null; _scene = null;
_timer = null;
} }
} }
} }

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d56be587f7b64f4aac094df08798619f
timeCreated: 1700625097

View File

@@ -0,0 +1,465 @@
using System;
using System.Collections.Generic;
namespace TEngine
{
public delegate void TimerHandler(object[] args);
[UpdateModule]
internal class TimerManager : ModuleImp
{
[Serializable]
internal class Timer
{
public int timerId = 0;
public float curTime = 0;
public float time = 0;
public TimerHandler Handler;
public bool isLoop = false;
public bool isNeedRemove = false;
public bool isRunning = false;
public bool isUnscaled = false; //是否使用非缩放的时间
public object[] Args = null; //回调参数
}
private int _curTimerId = 0;
private readonly List<Timer> _timerList = new List<Timer>();
private readonly List<Timer> _unscaledTimerList = new List<Timer>();
private readonly List<int> _cacheRemoveTimers = new List<int>();
private readonly List<int> _cacheRemoveUnscaledTimers = new List<int>();
/// <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)
{
Timer timer = new Timer
{
timerId = ++_curTimerId,
curTime = time,
time = time,
Handler = callback,
isLoop = isLoop,
isUnscaled = isUnscaled,
Args = args,
isNeedRemove = false,
isRunning = true
};
InsertTimer(timer);
return timer.timerId;
}
private void InsertTimer(Timer timer)
{
bool isInsert = false;
if (timer.isUnscaled)
{
for (int i = 0, len = _unscaledTimerList.Count; i < len; i++)
{
if (_unscaledTimerList[i].curTime > timer.curTime)
{
_unscaledTimerList.Insert(i, timer);
isInsert = true;
break;
}
}
if (!isInsert)
{
_unscaledTimerList.Add(timer);
}
}
else
{
for (int i = 0, len = _timerList.Count; i < len; i++)
{
if (_timerList[i].curTime > timer.curTime)
{
_timerList.Insert(i, timer);
isInsert = true;
break;
}
}
if (!isInsert)
{
_timerList.Add(timer);
}
}
}
/// <summary>
/// 暂停计时器。
/// </summary>
/// <param name="timerId">计时器Id。</param>
public void Stop(int timerId)
{
Timer timer = GetTimer(timerId);
if (timer != null) timer.isRunning = false;
}
/// <summary>
/// 恢复计时器。
/// </summary>
/// <param name="timerId">计时器Id。</param>
public void Resume(int timerId)
{
Timer timer = GetTimer(timerId);
if (timer != null) timer.isRunning = true;
}
/// <summary>
/// 计时器是否在运行中。
/// </summary>
/// <param name="timerId">计时器Id。</param>
/// <returns>否在运行中。</returns>
public bool IsRunning(int timerId)
{
Timer timer = GetTimer(timerId);
return timer is { isRunning: true };
}
/// <summary>
/// 获得计时器剩余时间
/// </summary>
public float GetLeftTime(int timerId)
{
Timer timer = GetTimer(timerId);
if (timer == null) return 0;
return timer.curTime;
}
/// <summary>
/// 重置计时器,恢复到开始状态。
/// </summary>
public void Restart(int timerId)
{
Timer timer = GetTimer(timerId);
if (timer != null)
{
timer.curTime = timer.time;
timer.isRunning = true;
}
}
/// <summary>
/// 重置计时器。
/// </summary>
public void Reset(int timerId, TimerHandler callback, float time, bool isLoop = false, bool isUnscaled = false)
{
Timer timer = GetTimer(timerId);
if (timer != null)
{
timer.curTime = time;
timer.time = time;
timer.Handler = callback;
timer.isLoop = isLoop;
timer.isNeedRemove = false;
if (timer.isUnscaled != isUnscaled)
{
RemoveTimerImmediate(timerId);
timer.isUnscaled = isUnscaled;
InsertTimer(timer);
}
}
}
/// <summary>
/// 重置计时器。
/// </summary>
public void Reset(int timerId, float time, bool isLoop, bool isUnscaled)
{
Timer timer = GetTimer(timerId);
if (timer != null)
{
timer.curTime = time;
timer.time = time;
timer.isLoop = isLoop;
timer.isNeedRemove = false;
if (timer.isUnscaled != isUnscaled)
{
RemoveTimerImmediate(timerId);
timer.isUnscaled = isUnscaled;
InsertTimer(timer);
}
}
}
/// <summary>
/// 立即移除。
/// </summary>
/// <param name="timerId"></param>
private void RemoveTimerImmediate(int timerId)
{
for (int i = 0, len = _timerList.Count; i < len; i++)
{
if (_timerList[i].timerId == timerId)
{
_timerList.RemoveAt(i);
return;
}
}
for (int i = 0, len = _unscaledTimerList.Count; i < len; i++)
{
if (_unscaledTimerList[i].timerId == timerId)
{
_unscaledTimerList.RemoveAt(i);
return;
}
}
}
/// <summary>
/// 移除计时器。
/// </summary>
/// <param name="timerId">计时器Id。</param>
public void RemoveTimer(int timerId)
{
for (int i = 0, len = _timerList.Count; i < len; i++)
{
if (_timerList[i].timerId == timerId)
{
_timerList[i].isNeedRemove = true;
return;
}
}
for (int i = 0, len = _unscaledTimerList.Count; i < len; i++)
{
if (_unscaledTimerList[i].timerId == timerId)
{
_unscaledTimerList[i].isNeedRemove = true;
return;
}
}
}
/// <summary>
/// 移除所有计时器。
/// </summary>
public void RemoveAllTimer()
{
_timerList.Clear();
_unscaledTimerList.Clear();
}
private Timer GetTimer(int timerId)
{
for (int i = 0, len = _timerList.Count; i < len; i++)
{
if (_timerList[i].timerId == timerId)
{
return _timerList[i];
}
}
for (int i = 0, len = _unscaledTimerList.Count; i < len; i++)
{
if (_unscaledTimerList[i].timerId == timerId)
{
return _unscaledTimerList[i];
}
}
return null;
}
private void LoopCallInBadFrame()
{
bool isLoopCall = false;
for (int i = 0, len = _timerList.Count; i < len; i++)
{
Timer timer = _timerList[i];
if (timer.isLoop && timer.curTime <= 0)
{
if (timer.Handler != null)
{
timer.Handler(timer.Args);
}
timer.curTime += timer.time;
if (timer.curTime <= 0)
{
isLoopCall = true;
}
}
}
if (isLoopCall)
{
LoopCallInBadFrame();
}
}
private void LoopCallUnscaledInBadFrame()
{
bool isLoopCall = false;
for (int i = 0, len = _unscaledTimerList.Count; i < len; i++)
{
Timer timer = _unscaledTimerList[i];
if (timer.isLoop && timer.curTime <= 0)
{
if (timer.Handler != null)
{
timer.Handler(timer.Args);
}
timer.curTime += timer.time;
if (timer.curTime <= 0)
{
isLoopCall = true;
}
}
}
if (isLoopCall)
{
LoopCallUnscaledInBadFrame();
}
}
private void UpdateTimer(float elapseSeconds)
{
bool isLoopCall = false;
for (int i = 0, len = _timerList.Count; i < len; i++)
{
Timer timer = _timerList[i];
if (timer.isNeedRemove)
{
_cacheRemoveTimers.Add(i);
continue;
}
if (!timer.isRunning) continue;
timer.curTime -= elapseSeconds;
if (timer.curTime <= 0)
{
if (timer.Handler != null)
{
timer.Handler(timer.Args);
}
if (timer.isLoop)
{
timer.curTime += timer.time;
if (timer.curTime <= 0)
{
isLoopCall = true;
}
}
else
{
_cacheRemoveTimers.Add(i);
}
}
}
for (int i = _cacheRemoveTimers.Count - 1; i >= 0; i--)
{
_timerList.RemoveAt(_cacheRemoveTimers[i]);
_cacheRemoveTimers.RemoveAt(i);
}
if (isLoopCall)
{
LoopCallInBadFrame();
}
}
private void UpdateUnscaledTimer(float realElapseSeconds)
{
bool isLoopCall = false;
for (int i = 0, len = _unscaledTimerList.Count; i < len; i++)
{
Timer timer = _unscaledTimerList[i];
if (timer.isNeedRemove)
{
_cacheRemoveUnscaledTimers.Add(i);
continue;
}
if (!timer.isRunning) continue;
timer.curTime -= realElapseSeconds;
if (timer.curTime <= 0)
{
if (timer.Handler != null)
{
timer.Handler(timer.Args);
}
if (timer.isLoop)
{
timer.curTime += timer.time;
if (timer.curTime <= 0)
{
isLoopCall = true;
}
}
else
{
_cacheRemoveUnscaledTimers.Add(i);
}
}
}
for (int i = _cacheRemoveUnscaledTimers.Count - 1; i >= 0; i--)
{
_unscaledTimerList.RemoveAt(_cacheRemoveUnscaledTimers[i]);
_cacheRemoveUnscaledTimers.RemoveAt(i);
}
if (isLoopCall)
{
LoopCallUnscaledInBadFrame();
}
}
private readonly List<System.Timers.Timer> _ticker = new List<System.Timers.Timer>();
public System.Timers.Timer AddSystemTimer(Action<object, System.Timers.ElapsedEventArgs> callBack)
{
int interval = 1000;
var timerTick = new System.Timers.Timer(interval);
timerTick.AutoReset = true;
timerTick.Enabled = true;
timerTick.Elapsed += new System.Timers.ElapsedEventHandler(callBack);
_ticker.Add(timerTick);
return timerTick;
}
private void DestroySystemTimer()
{
foreach (var ticker in _ticker)
{
if (ticker != null)
{
ticker.Stop();
}
}
}
internal override void Update(float elapseSeconds, float realElapseSeconds)
{
UpdateTimer(elapseSeconds);
UpdateUnscaledTimer(realElapseSeconds);
}
internal override void Shutdown()
{
RemoveAllTimer();
DestroySystemTimer();
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 3a3b960842a44c6fbba975083ba5fa50
timeCreated: 1700625107

View File

@@ -0,0 +1,170 @@
using UnityEngine;
namespace TEngine
{
/// <summary>
/// 计时器模块。
/// </summary>
[DisallowMultipleComponent]
public sealed partial class TimerModule : Module
{
private TimerManager _timerManager;
/// <summary>
/// 游戏框架组件初始化。
/// </summary>
protected override void Awake()
{
base.Awake();
_timerManager = ModuleImpSystem.GetModule<TimerManager>();
if (_timerManager == null)
{
Log.Fatal("TimerMgr is invalid.");
}
}
/// <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);
}
/// <summary>
/// 计时器是否在运行中。
/// </summary>
/// <param name="timerId">计时器Id。</param>
/// <returns>否在运行中。</returns>
public bool IsRunning(int timerId)
{
if (_timerManager == null)
{
throw new GameFrameworkException("TimerMgr is invalid.");
}
return _timerManager.IsRunning(timerId);
}
/// <summary>
/// 获得计时器剩余时间。
/// </summary>
public float GetLeftTime(int timerId)
{
if (_timerManager == null)
{
throw new GameFrameworkException("TimerMgr is invalid.");
}
return _timerManager.GetLeftTime(timerId);
}
/// <summary>
/// 重置计时器,恢复到开始状态。
/// </summary>
public void Restart(int timerId)
{
if (_timerManager == null)
{
throw new GameFrameworkException("TimerMgr is invalid.");
}
_timerManager.Restart(timerId);
}
/// <summary>
/// 重置计时器。
/// </summary>
public void ResetTimer(int timerId, TimerHandler callback, float time, bool isLoop = false, bool isUnscaled = false)
{
if (_timerManager == null)
{
throw new GameFrameworkException("TimerMgr is invalid.");
}
_timerManager.Reset(timerId,callback,time,isLoop,isUnscaled);
}
/// <summary>
/// 重置计时器。
/// </summary>
public void ResetTimer(int timerId, float time, bool isLoop, bool isUnscaled)
{
if (_timerManager == null)
{
throw new GameFrameworkException("TimerMgr is invalid.");
}
_timerManager.Reset(timerId, time,isLoop,isUnscaled);
}
/// <summary>
/// 移除计时器。
/// </summary>
/// <param name="timerId">计时器Id。</param>
public void RemoveTimer(int timerId)
{
if (_timerManager == null)
{
Log.Fatal("TimerMgr is invalid.");
throw new GameFrameworkException("TimerMgr is invalid.");
}
_timerManager.RemoveTimer(timerId);
}
/// <summary>
/// 移除所有计时器。
/// </summary>
public void RemoveAllTimer()
{
if (_timerManager == null)
{
Log.Fatal("TimerMgr is invalid.");
throw new GameFrameworkException("TimerMgr is invalid.");
}
_timerManager.RemoveAllTimer();
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 5e79c115d5054209810f42dc6e25cf94
timeCreated: 1700625348