首页 > 代码库 > 定时器

定时器

using System.Collections.Generic;
using UnityEngine;

public class Timer
{
    //自动释放
    private readonly bool autoRelease;
    //回调
    private readonly TimerMngr.Callback func;
    //间隔时间
    private float interval;
    //重复次数
    private int repeat;
    private bool started;
    //时间计数
    private float time;

    public Timer(TimerMngr.Callback _func)
    {
        time = 0f;
        repeat = 0;
        func = _func;
        autoRelease = true;
    }

    public Timer(TimerMngr.Callback _func, int repeatTimes, bool autoR)
    {
        time = 0f;
        func = _func;
        autoRelease = autoR;
        repeat = repeatTimes;
    }

    public Timer(TimerMngr.Callback _func, bool autoR)
    {
        time = 0f;
        func = _func;
        autoRelease = autoR;
        repeat = 0;
    }

    public void Start(float _time)
    {
        time = 0f;
        interval = _time;
        started = true;
    }

    public void Stop()
    {
        time = 0f;
        started = false;
    }

    public bool TimesUp()
    {
        return time >= interval;
    }
    public bool FixedUpdate()
    {
        if (started == false)
            return false;

        time += Time.deltaTime;
        if (!TimesUp())
            return false;
        //时间到了
        if (func != null)
        {
            func();
            if (repeat <= 1)
            {
                Stop();
                return autoRelease;
            }
            repeat--;
            time = 0;
            return false;
        }
        Stop();
        return true;
    }
}

public class TimerMngr : Singleton<TimerMngr>
{
    public delegate void Callback();

    private readonly LinkedList<Timer> timerList = new LinkedList<Timer>();

    public Timer Create(Callback func)
    {
        var timer = new Timer(func);
        timerList.AddLast(timer);
        return timer;
    }

    public Timer Create(Callback func, bool autoRelease)
    {
        var timer = new Timer(func, autoRelease);
        timerList.AddLast(timer);
        return timer;
    }

    public Timer Create(Callback func, int repeatTimes, bool autoRelease)
    {
        var timer = new Timer(func, repeatTimes, autoRelease);
        timerList.AddLast(timer);
        return timer;
    }

    public void Destroy(Timer timer)
    {
        timerList.Remove(timer);
    }

    public void FixedUpdate()
    {
        var first = timerList.First;
        while (first != null)
        {
            if (first.Value.FixedUpdate())
            {
                var node = first;
                first = first.Next;
                timerList.Remove(node);
            }
            else
            {
                first = first.Next;
            }
        }
    }
}
public class test : MonoBehaviour
{

    // Use this for initialization
    void Start()
    {
        TimerMngr.Instance.Create(() =>
        {
            print("两秒后回调回来");
        }).Start(2);
    }

    public void FixedUpdate()
    {
        TimerMngr.Instance.FixedUpdate();
    }
}

 

定时器