当前位置:网站首页>Unity 使用双缓冲实现一个好用的计时器
Unity 使用双缓冲实现一个好用的计时器
2022-08-11 05:31:00 【canon_卡农】
先贴出代码:
public class Timer
{
// 双缓冲,防止计时器回调时更改计时器队列
List<Schedule> mFront = new List<Schedule>();
List<Schedule> mBack = new List<Schedule>();
List<Schedule> mGarbrage = new List<Schedule>();
public static Timer Instance
{
get {
return mSingleton; }
}
static readonly Timer mSingleton = new Timer();
public Schedule SetSchedule(float time, System.Action<Schedule> func)
{
Schedule sc = new Schedule(time, func);
mFront.Add(sc);
return sc;
}
public Schedule SetSchedule(float time, bool usingWorldTimeScale, System.Action<Schedule> func)
{
Schedule sc = new Schedule(time, usingWorldTimeScale, func);
mFront.Add(sc);
return sc;
}
public void RemoveSchedule(Schedule sc)
{
if (sc == null) return;
mFront.Remove(sc);
mBack.Remove(sc);
}
public void SetSchedule(Schedule sc)
{
if (mBack.Contains(sc))
{
return;
}
if (!mFront.Contains(sc))
{
mFront.Add(sc);
}
}
public void Update()
{
if (mFront.Count > 0)
{
mBack.AddRange(mFront);
mFront.Clear();
}
float dt = Time.deltaTime;
for (int i = 0; i < mBack.Count; i++)
{
if (mBack[i].Canceled)
{
mGarbrage.Add(mBack[i]);
continue;
}
Schedule sc = mBack[i];
float tmpTime = sc.Time;
// TODO: if game is pause,InGameTimeScale value is 0.0f;
float InGameTimeScale = 1.0f;
tmpTime -= sc.UsingWorldTimeScale ? (dt * InGameTimeScale) : dt;
//tmpTime -= dt;
sc.ResetTime(tmpTime);
if (tmpTime <= 0)
{
if (sc.Callback != null)
{
sc.Callback(sc);
}
if (sc.Time <= 0)
{
mGarbrage.Add(sc);
}
}
}
for (int i = 0; i < mGarbrage.Count; i++)
{
if (mBack.Contains(mGarbrage[i]))
{
mBack.Remove(mGarbrage[i]);
}
}
mGarbrage.Clear();
}
}
public class Schedule
{
float mTime = 0.0001f;
System.Action<Schedule> mDelegate;
bool mCanceled = false;
bool mUseWorldTimeScale = false;
public bool Canceled
{
get
{
return mCanceled;
}
}
public float Time
{
get {
return mTime; }
}
public bool UsingWorldTimeScale
{
get {
return mUseWorldTimeScale; }
}
public System.Action<Schedule> Callback
{
get {
return mDelegate; }
}
public Schedule()
{
}
public Schedule(float t, System.Action<Schedule> callback)
{
mTime = t;
mDelegate = callback;
}
public Schedule(float t, bool worldTimeScale, System.Action<Schedule> callback)
{
mTime = t;
mUseWorldTimeScale = worldTimeScale;
mDelegate = callback;
}
public void Cancel()
{
mCanceled = true;
}
public void ResetTime(float t)
{
mTime = t;
}
}
使用的时候只需在一个Mono脚本的Update去调用Timer的Update函数,就会每帧遍历计时器,并在每个计时器的 Time <= 0 的时候执行回调。
边栏推荐
- JS advanced web page special effects (pink teacher notes)
- Scene-driven feature calculation method OpenMLDB, efficient implementation of "calculate first use"
- Intelligent risk control China design and fall to the ground
- Jetpack use exception problem collection
- Fourth Paradigm OpenMLDB optimization innovation paper was accepted by VLDB, the top international database association
- Day 68
- 【LeetCode-414】第三大的数
- C语言-7月31日-指针的总结以及typedef关键字
- JVM学习四:垃圾收集器与内存回收策略
- The official website of OpenMLDB is upgraded, and the mysterious contributor map will take you to advance quickly
猜你喜欢
随机推荐
解决AttributeError: ‘NoneType‘ object has no attribute ‘val‘ if left.val!=right.val:Line 17 问题
heap2 (tcache attack,house of orange)
OpenMLDB: Consistent production-level feature computing platform online and offline
Intelligent risk control China design and fall to the ground
深度学习Matlab工具箱代码注释
【LeetCode-56】合并区间
C语言-内存操作函数
JVM tuning and finishing
Promise.race learning (judging the fastest execution of multiple promise objects)
【LeetCode-349】两个数组的交集
mysql basic summary
vim 编辑器使用学习
js learning advanced (event senior pink teacher teaching notes)
Day 69
Dark Horse Event Project
mk file introduction
Day 81
The role of the port
Use the adb command to manage applications
【LeetCode-36】有效的数独