Unity 定时器管理类 拖上去就能用哦~

下面有源码 可直接拷贝哦

使用步骤( 如何料理 )

如何创建?
我们需要在场景里新建要一个 GameObject类 来挂我们的SchedulerMgr脚本

如何去调用
1.注册 2.获取 3.移除

方法 功能 描述
SchedulerMgr.scheduleOnce 仅调度一次的定时器 一般情况 用于延时调用一个方法 <静态方法>
SchedulerMgr.schedule 注册一个定时器 循环 间隔 调度一个方法, 当限定次数为-1时 永远调度 <静态方法>
SchedulerMgr.getSchedulerByTag 获取某一个定时器的状态 在创建任意定时器时都会返回一个tag <静态方法>
SchedulerMgr.removeByTag 移除一个定时器 当你想立即停止一个定时器的调度时 可以用移除方法 <静态方法>
using System.Collections;
using System.Collections.Generic;
using UnityEngine;




/// <summary>
/// 定时器管理类
/// </summary>
public class SchedulerMgr : MonoBehaviour
{
    
    

    public delegate void SchedulerAction(Scheduler scheduler);
    static List<Scheduler> schedules = new List<Scheduler>();
    static int tagCount = 1000;

    private void Update()
    {
    
    
        if (schedules.Count == 0) return;

        float dt = Time.deltaTime;
        Scheduler pCur = null;
        for (int i = 0; i < schedules.Count; ++i)
        {
    
    
            pCur = schedules[i];
            if (pCur == null)
            {
    
    
                remove(i); --i;
                continue;
            }

            pCur.tm += Time.deltaTime;
            if (pCur.tm >= pCur.life)
            {
    
    
                pCur.func(pCur);
                pCur.tm -= pCur.life;

                if (pCur.count != -1)
                {
    
    
                    --pCur.count;
                    if (pCur.count == 0)
                    {
    
    
                        remove(i); --i;
                    }
                }
            }
        }
    }

    #region 注册
    /// <summary>
    /// 延时调度
    /// 注册一个 仅调度一次的定时器
    /// </summary>
    /// <param name="delay">等待时间 单位秒</param>
    /// <param name="func">调度方法</param>
    /// <returns>每个调度器的标签值</returns>
    public static int scheduleOnce(float delay, SchedulerAction func)
    {
    
    
        return schedule(delay, func, 1);
    }


    /// <summary>
    /// 注册一个定时器
    /// </summary>
    /// <param name="interval">间隔 单位秒</param>
    /// <param name="func">调度方法</param>
    /// <param name="count">调度次数  如果为-1 则永远调度</param>
    /// <returns>每个调度器的标签值</returns>
    public static int schedule(float interval, SchedulerAction func, int count = -1)
    {
    
    

        ++tagCount;
        var scheduler = new Scheduler();
        scheduler.tm = 0;
        scheduler.life = interval;
        scheduler.func = func;
        scheduler.count = count;
        scheduler.tag = tagCount;
        schedules.Add(scheduler);

        return tagCount;
    }
    #endregion

    #region 获取
    public static Scheduler getSchedulerByTag(int tag)
    {
    
    
        
        foreach(var scheduler in schedules)
        {
    
    
            if(tag == scheduler.tag)
            {
    
    
                return scheduler;
            }
        }

        return null;
    }
    

    #endregion

    #region 移除
    static Scheduler remove(int index)
    {
    
    
        if (index >= 0 && index < schedules.Count)
        {
    
    
            var temp = schedules[index];
            schedules[index] = schedules[schedules.Count - 1];
            schedules.RemoveAt(schedules.Count - 1);
            return temp;
        }
        return null;
    }
    public static Scheduler removeByTag(int tag)
    {
    
    
        for(int i= 0; i < schedules.Count; i++)
        {
    
    
            if(schedules[i].tag == tag)
            {
    
    
                return remove(i);
            }
        }
        return null;
    }
    #endregion
}


public class Scheduler
{
    
    
    public int tag;
    public float tm;
    public float life;
    public int count;
    public SchedulerMgr.SchedulerAction func;
}



Guess you like

Origin blog.csdn.net/qq_39162566/article/details/113105351