Unity 使用双缓冲实现一个好用的计时器

先贴出代码:

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 的时候执行回调。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值