Unity定时任务管理器

Unity定时任务管理器,可添加、移除定时任务


using System.Collections.Generic;
using UnityEngine;

public delegate void TimeTaskDelegate();

/// 
/// 定时任务管理器
/// 
public class TimeManager :MonoBehaviour{
    /// 
    /// 定时任务列表
    /// 
    private List taskList = new List();

    private static TimeManager _timeInstance;
    public static TimeManager timeInstance
    {
        get
        {
            if (_timeInstance == null)
            {
                _timeInstance = FindObjectOfType(typeof(TimeManager)) as TimeManager;
                if(_timeInstance == null)
                {
                    GameObject obj = new GameObject("TimeManager");
                    obj.hideFlags = HideFlags.HideAndDontSave;
                    _timeInstance = (TimeManager)obj.AddComponent();  
                }
                return _timeInstance;
            }
            return _timeInstance;
        }
    }

    void Start()
    {
        DontDestroyOnLoad(this.gameObject);
        if (_timeInstance == null)
            _timeInstance = this as TimeManager;
    }
    
    /// 
    /// 添加定时任务
    /// 
    /// 延时执行时间间隔
    /// 是否可以重复执行
    /// 执行回调
    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);
    }

    /// 
    /// 移除定时任务
    /// 
    /// 
    /// 
    public bool removeTask(TimeTaskDelegate taskToRemove)
    {
        if (taskList.Count == 0 || taskToRemove == null) return false;
        foreach(TimeTask item in taskList)
        {
            if (item.timeTaskCallBack == taskToRemove)
                return taskList.Remove(item);
        }
        return false;
    }

    void Update()
    {
        Tick();
    }

    /// 
    /// 执行定时任务
    /// 
    private void Tick()
    {
        if (taskList == null) return;
        foreach (TimeTask task in taskList)
        {
            task.timeDelay -= Time.deltaTime;
            if (task.timeDelay <= 0)
            {
                task.timeTaskCallBack();
                task.timeDelay = task.timeDelayOnly;
            }
            if (!task.repeat)
                taskList.Remove(task);
        }
    }
}

/// 
/// 定时任务封装类
/// 
public class TimeTask
{
    /// 
    /// 延迟时间
    /// 
    private float _timeDelay;
    /// 
    /// 延迟时间
    /// 
    private float _timeDelayOnly;
    /// 
    /// 是否需要重复执行
    /// 
    private bool _repeat;
    /// 
    /// 回调函数
    /// 
    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) { }
}


测试


using UnityEngine;

public class TimeMgrTest : MonoBehaviour
{
    void Start()
    {
        TimeManager.timeInstance.addTask(5.0f, true, GetData);
    }
    public void GetData()
    {
        Debug.Log("get data form database ! time now :" + Time.time);
    }
}

Unity定时任务管理器_第1张图片


你可能感兴趣的:(Unity)