01定时回调

/****************************************************
    文件:TimeTask.cs
	作者:唐孝辉    邮箱: 1351105506@qq.com
    日期:#CreateTime#
	功能:任务类
*****************************************************/

using System;


/// <summary>
/// 时间类型
/// </summary>
public enum TimeUnit
{
    MilliSecond, //毫秒
    Second, //秒
    Minute, //分钟
    Hour,//小时
    Day,//天
}

public class TimeTask
{
    public Action callBack;
    public float destTime;//要达到的时间点

    public TimeTask(Action callBack, float destTime,TimeUnit timeUnit)
    {
        this.callBack = callBack;
        this.destTime = destTime;
    }
}

/****************************************************
    文件:TimeSys.cs
	作者:唐孝辉    邮箱: 1351105506@qq.com
    日期:#CreateTime#
	功能:定时系统
*****************************************************/

using System;
using System.Collections.Generic;
using UnityEngine;

public class TimerSys : MonoBehaviour
{

    private List<TimeTask> cacheTaskList=new List<TimeTask>(); //缓存
    private List<TimeTask> taskList=new List<TimeTask>();


    void Update()
    {
        foreach (TimeTask task in cacheTaskList)
        {
            taskList.Add(task);
        }
        cacheTaskList.Clear();

  
        for (int i = 0; i < taskList.Count; i++)
        {
            TimeTask timeTask = taskList[i];
            //判断是否足条件
            if (Time.realtimeSinceStartup*1000<timeTask.destTime)
            {
                continue;
            }
            else
            {
                //执行任务
                if (timeTask.callBack!=null)
                {
                    timeTask.callBack.Invoke();
                }
                //任务完成移除任务
                taskList.RemoveAt(i);
                i--;
            }
            
        }
    }

    //默认是毫秒
    public void AddTimeTask(Action callBack,float delayTime,TimeUnit type=TimeUnit.MilliSecond)
    {

        if (type != TimeUnit.MilliSecond)
        {
            switch (type)
            {
                case TimeUnit.Second:
                    delayTime = 1000 * delayTime;
                    break;
                case TimeUnit.Minute:
                    delayTime = delayTime * 1000 * 60;
                    break;
                case TimeUnit.Hour:
                    delayTime = delayTime * 1000 * 60 * 60;
                    break;
                case TimeUnit.Day:
                    delayTime = delayTime * 1000 * 60 * 60 * 24;
                    break;
            }

        }
        TimeTask timeTask=new TimeTask(callBack,delayTime+Time.realtimeSinceStartup*1000,type);
        //添加到缓存任务里
        cacheTaskList.Add(timeTask);
    }
}
/****************************************************
    文件:GameRoot.cs
	作者:唐孝辉    邮箱: 1351105506@qq.com
    日期:#CreateTime#
	功能:GameRoot
*****************************************************/

using System.Collections.Generic;
using UnityEngine;

public class GameRoot : MonoBehaviour
{
    private TimerSys timerSys;
    void Start()
     {
        timerSys = this.GetComponent<TimerSys>();
    }


    //开始任务
    public void ClickTaskBtn()
    {
        timerSys.AddTimeTask(()=>{Debug.Log("TestA"); },2000);
    }
    
}

在这里插入图片描述

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值