定时器

本文介绍了一个基于Unity的简易定时器组件实现方案。该方案利用C#语言创建了一个可重复使用的Timer类,能够设定间隔时间及重复次数,并通过TimerMngr类进行统一管理。文章还提供了一个测试场景用例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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();
    }
}

 

转载于:https://www.cnblogs.com/zjw007/p/6127389.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值