当前位置:网站首页>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 learning advanced (event senior pink teacher teaching notes)
C语言-6月8日-给定一个字符数组‘i am a student’ 统计字符a的个数并进行输出
厂商推送平台-华为接入
【剑指offer系列】面试题日记(前期题)
【LeetCode-349】两个数组的交集
OpenGL 摄像机(Camera)类的创建
stack stack
Fourth Paradigm OpenMLDB optimization innovation paper was accepted by VLDB, the top international database association
JS进阶网页特效(pink老师笔记)
2022DASCTF X SU 三月春季挑战赛 checkin ROPgadget进阶使用
随机推荐
Byte (byte) and bit (bit)
【无标题】
C语言-6月8日-给定一个字符数组‘i am a student’ 统计字符a的个数并进行输出
【LeetCode-349】两个数组的交集
ARM assembly instruction ADR and LDR
Day 76
Lua 快速入门(五)——协程(thread)
Scene-driven feature calculation method OpenMLDB, efficient implementation of "calculate first use"
解决npm warn config global `--global`, `--local` are deprecated. use `--location=global` instead.
C语言-7月21日-指针的深入
IIC and SPI
一文看懂注解与反射
OpenMLDB + Jupyter Notebook: Quickly Build Machine Learning Applications
USB in NRZI to encode the data
OpenGL中glGenBuffers glBindBuffer glBufferData的理解
Unity 数字跳字功能
深度学习Matlab工具箱代码注释
C语言-内存操作函数
swagger错误:WARN i.s.m.p.AbstractSerializableParameter - [getExample,421] - Illegal DefaultValue null
【LeetCode-69】x的平方根