天天看點

04定時回調增加任務替換功能

/****************************************************
    檔案:TimeTask.cs
    作者:唐孝輝 
*****************************************************/

using System;


/// <summary>
/// 時間類型
/// </summary>
public enum TimeUnit
{
    MilliSecond, //毫秒
    Second, //秒
    Minute, //分鐘
    Hour,//小時
    Day,//天
}

public class TimeTask
{
    public int taskID;
    public Action callBack;
    public float destTime;//要達到的時間點
    public int count;//循環的次數
    public float delay;//下一次循環延遲的時間
    public TimeUnit timeUnit;
    public TimeTask(int taskId,Action callBack, float destTime,int count,float delay,TimeUnit timeUnit)
    {
        this.taskID = taskId;
        this.callBack = callBack;
        this.destTime = destTime;
        this.count = count;
        this.delay = delay;
        this.timeUnit = timeUnit;
    }
}      
/****************************************************
    檔案:TimeSys.cs
    作者:唐孝輝   
*****************************************************/

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

public class TimerSys : MonoBehaviour
{

    private List<TimeTask> cacheTaskList=new List<TimeTask>(); //緩存
    private List<TimeTask> taskList=new List<TimeTask>();
    private int taskid = 0;
    private static readonly string obj = "22";
    private List<int> idList=new List<int>();
    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();
                }

                if (timeTask.count==1)
                {
                    //任務完成移除任務
                    taskList.RemoveAt(i);
                    i--;
                    break;
                }
                else
                {
                    if (timeTask.count!=0)
                    {
                        timeTask.count -= 1;
                    }
                       //時間還要增加  
                    timeTask.destTime += timeTask.delay;
                }
            }
            
        }
    }

    //預設是毫秒
    public int AddTimeTask(Action callBack,float delayTime,int count, float delay,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;
            }

        }

        int id=GetTaskID();
        TimeTask timeTask=new TimeTask(id,callBack,delayTime+Time.realtimeSinceStartup*1000,count,delay,type);
        //添加到緩存任務裡
        cacheTaskList.Add(timeTask);
        idList.Add(id);
        return id;
    }


    //給任務配置設定id
    private int GetTaskID()
    {
        lock (obj)
        {
            taskid += 1;
            while (true)
            {
                if (taskid >= int.MaxValue)
                {
                    taskid = 0;
                }

                bool idIsUse = false;
                for (int i = 0; i < idList.Count; i++)
                {
                    if (idList[i] == taskid)
                    {
                        idIsUse = true;
                        break;
                    }
                }

                if (!idIsUse)
                {
                    break;
                }
                else
                {
                    taskid += 1;
                }
            }
           
        }

        return taskid;
    }

    //删除任務
    public  bool  DeleteTimeTask(int taskID)
    {
        bool idIsExist = false;
        for (int i = 0; i < taskList.Count; i++)
        {
            if (taskList[i].taskID==taskID)
            {
                //移除任務和id
                taskList.RemoveAt(i);
                for (int j = 0; j <idList.Count; j++)
                {
                    if (idList[j]==taskID)
                    {
                        idList.RemoveAt(j);
                        break;
                    }
                }

                idIsExist = true;
                break;
            }
        }

        if (!idIsExist)
        {
            for (int i = 0; i < cacheTaskList.Count; i++)
            {
                if (cacheTaskList[i].taskID == taskID)
                {
                    //移除任務和id
                    cacheTaskList.RemoveAt(i);
                    for (int j = 0; j < idList.Count; j++)
                    {
                        if (idList[j] == taskID)
                        {
                            idList.RemoveAt(j);
                            break;
                        }
                    }

                    idIsExist = true;
                    break;
                }
            }
        }

        return idIsExist;
    }


    //替換任務
    public bool ReplaceTimeTask(int id,Action callBack, float delayTime, int count, float delay, 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 newTask = new TimeTask(GetTaskID(), callBack, delayTime + Time.realtimeSinceStartup * 1000, count, delay, type);

        bool isReplace = false;
        for (int i = 0; i < taskList.Count; i++)
        {
            if (taskList[i].taskID==id)
            {
                taskList[i] = newTask;
                isReplace = true;
                break;
            }
        }

        if (!isReplace)
        {
            for (int i = 0; i < cacheTaskList.Count; i++)
            {
                if (cacheTaskList[i].taskID == id)
                {
                    cacheTaskList[i] = newTask;
                    isReplace = true;
                    break;
                }
            }
        }

        return isReplace;
    }

}      
/****************************************************
    檔案:GameRoot.cs
    作者:唐孝輝 
*****************************************************/

using System.Collections.Generic;
using UnityEngine;

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


    //開始任務
    public void ClickTaskBtn()
    {
        taskId = timerSys.AddTimeTask(()=>{Debug.Log("TestA"); },2000,5,500,TimeUnit.MilliSecond);
    }
    //删除任務
    public void ClickDeleteTaskBtn()
    {
        bool success=timerSys.DeleteTimeTask(taskId);
        Debug.Log("移除id"+taskId+":"+success);
    }
    //替換任務
    public void ClickReplaceTaskBtn()
    {
        bool success= timerSys.ReplaceTimeTask(taskId, () => { Debug.Log("TestB"); }, 2000, 5, 500, TimeUnit.MilliSecond);
        Debug.Log("替換的任務的id" + taskId + ":" + success);
    }
}