unity3d:定时任务封装类TimeManager

2023-08-24 14:33:24 浏览数 (1)

代码语言:javascript复制
namespace Framework.Tools
{
    /// <summary>  
    /// 定时任务封装类  
    /// </summary>  
    public class TimeTask
    {
        /// <summary>  
        /// 延迟时间  
        /// </summary>  
        private float _timeDelay;
        /// <summary>  
        /// 延迟时间  
        /// </summary>  
        private float _timeDelayOnly;
        /// <summary>  
        /// 是否需要重复执行  
        /// </summary>  
        private bool _repeat;
        /// <summary>  
        /// 回调函数  
        /// </summary>  
        private TimeTaskDelegate _timeTaskCallBack;

        public float timeDelay
        {
            get { return _timeDelay; }
            set { _timeDelay = value; }
        }
        public float timeDelayOnly
        {
            get { return _timeDelayOnly; }
        }
        public bool repeat
        {
            get { return _repeat; }
            set { _repeat = value; }
        }
        public TimeTaskDelegate timeTaskCallBack
        {
            get { return _timeTaskCallBack; }
        }

        //构造函数  
        public TimeTask(float timeDelay, bool repeat, TimeTaskDelegate timeTaskCallBack)
        {
            _timeDelay = timeDelay;
            _timeDelayOnly = timeDelay;
            _repeat = repeat;
            _timeTaskCallBack = timeTaskCallBack;
        }

        public TimeTask(float timeDelay, TimeTaskDelegate timeTaskCallBack) : this(timeDelay, true, timeTaskCallBack) { }
    }
}

代码语言:javascript复制
using System.Collections.Generic;  
using UnityEngine;
using Framework.Pattern;

namespace Framework.Tools
{
    public delegate void TimeTaskDelegate();

    /// <summary>  
    /// 定时任务管理器  
    /// </summary>  
    public class TimeManager : SingletonMono<TimeManager>
    {
        /// <summary>  
        /// 定时任务列表  
        /// </summary>  
        private List<TimeTask> taskList = new List<TimeTask>();

        /// <summary>  
        /// 添加定时任务  
        /// </summary>  
        /// <param name="timeDelay">延时执行时间间隔</param>  
        /// <param name="repeat">是否可以重复执行</param>  
        /// <param name="timeTaskCallback">执行回调</param>  
        public void AddTask(float timeDelay, bool repeat, TimeTaskDelegate timeTaskCallback)
        {
            AddTask(new TimeTask(timeDelay, repeat, timeTaskCallback));
        }
        public void AddTask(TimeTask taskToAdd)
        {
            if (taskList.Contains(taskToAdd) || taskToAdd == null) return;
            taskList.Add(taskToAdd);
        }

        /// <summary>  
        /// 移除定时任务  
        /// </summary>  
        /// <param name="taskToRemove"></param>  
        /// <returns></returns>  
        public bool RemoveTask(TimeTaskDelegate taskToRemove)
        {
            if (taskList.Count == 0 || taskToRemove == null) return false;
            for (var i = 0; i < taskList.Count; i  )
            {
                TimeTask item = taskList[i];
                if (item.timeTaskCallBack == taskToRemove)
                    return taskList.Remove(item);
            }
            return false;
        }

        void Update()
        {
            Tick();
        }

        /// <summary>  
        /// 执行定时任务  
        /// </summary>  
        private void Tick()
        {
            if (taskList == null) return;
            for (var i = 0; i < taskList.Count;)
            {
                TimeTask task = taskList[i];
                task.timeDelay -= Time.deltaTime;
                if (task.timeDelay <= 0)
                {
                    if (task.timeTaskCallBack != null)
                    {
                        task.timeTaskCallBack();
                    }
                    if (!task.repeat)
                    {
                        taskList.Remove(task);
                        continue;
                    }
                    task.timeDelay = task.timeDelayOnly;
                }
                i  ;
            }
        }
    }
}

0 人点赞