本文共 10740 字,大约阅读时间需要 35 分钟。
/**************************************************** 文件:PETimer.cs 作者:唐孝辉 邮箱: 1351105506@qq.com 日期:#CreateTime# 功能:Nothing*****************************************************/using System;using System.Collections.Generic;public class PETimer{ private int id = 0; private List idList = new List (); //所有的任务id的集合 private List cacheIdList = new List (); private static readonly string obj = "233"; private ListcacheTimeTasks = new List (); private List timeTasks = new List (); private List cacheFrameTasks = new List (); //帧缓存 private List frameTasks = new List (); private DateTime starTime=new DateTime(1970,1,1,0,0,0,0); private Action action = null; private Double nowTime; public PETimer() { idList.Clear(); cacheIdList.Clear(); cacheTimeTasks.Clear(); timeTasks.Clear(); cacheFrameTasks.Clear(); frameTasks.Clear(); } public void SetLog(Action action) { this.action = action; } public void LogInfo(string str) { if (action!=null) { action.Invoke(str); } } //获取时间间隔毫秒记 public double GetNowTime() { TimeSpan timeSpan=DateTime.UtcNow - starTime; return timeSpan.TotalMilliseconds; } public void Update() { ExecuteFrameTask(); ExecuteTask(); //移除任务已经行完的id if (cacheIdList.Count > 0) { for (int i = 0; i < cacheIdList.Count; i++) { int id = cacheIdList[i]; for (int j = 0; j < idList.Count; j++) { if (idList[i] == id) { idList.RemoveAt(j); break; } } } } cacheIdList.Clear(); } #region 任务 //执行任务 private void ExecuteTask() { //从缓存里里面取出来 if (cacheTimeTasks.Count > 0) { foreach (TimeTask task in cacheTimeTasks) { timeTasks.Add(task); } } cacheTimeTasks.Clear(); for (int i = 0; i < timeTasks.Count; i++) { TimeTask timeTask = timeTasks[i]; nowTime = GetNowTime(); if (nowTime.CompareTo(timeTask.destTime)<0) { continue; } else { if (timeTask.callBack != null) { timeTask.callBack.Invoke(); } //到达时间了 //判断任务执行的次数 if (timeTask.count == 1) { //移除任务 timeTasks.RemoveAt(i); i--; cacheIdList.Add(timeTask.taskID); break; } else { if (timeTask.count != 0) { timeTask.count -= 1; } //时间加 timeTask.destTime += timeTask.delayTime; } } } } //添加任务 默认是毫秒 返回添加任务的id public int AddTask(double destTime, double delayTime, Action callBack, TimeUnit timeUnit = TimeUnit.MilliSecond, int count = 1) { //时间 if (timeUnit != TimeUnit.MilliSecond) { switch (timeUnit) { case TimeUnit.Second: delayTime = delayTime * 1000; 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 taskID = GettaskId(); nowTime = GetNowTime(); destTime += nowTime;//目标时间 TimeTask timeTask = new TimeTask(taskID, count, destTime, delayTime, callBack, timeUnit); //添加到idList里 //添加到缓存里 idList.Add(taskID); cacheTimeTasks.Add(timeTask); return taskID; } //删除任务 public bool DeleteTask(int taskId) { //寻找任务 bool isDelete = false; for (int i = 0; i < cacheTimeTasks.Count; i++) { if (cacheTimeTasks[i].taskID == taskId) { cacheTimeTasks.RemoveAt(i); isDelete = true; break; } } if (!isDelete) { for (int i = 0; i < timeTasks.Count; i++) { if (timeTasks[i].taskID == taskId) { timeTasks.RemoveAt(i); isDelete = true; break; } } } return isDelete; } //替换任务 public bool ReplaceTask(int id, double destTime, double delayTime, Action callBack, TimeUnit timeUnit = TimeUnit.MilliSecond, int count = 1) { //构造新任务 //时间 if (timeUnit != TimeUnit.MilliSecond) { switch (timeUnit) { case TimeUnit.Second: destTime = destTime * 1000; break; case TimeUnit.Minute: destTime = destTime * 1000 * 60; break; case TimeUnit.Hour: destTime = destTime * 1000 * 60 * 60; break; case TimeUnit.Day: destTime = destTime * 1000 * 60 * 60 * 24; break; } } int taskID = GettaskId(); nowTime = GetNowTime(); destTime += nowTime; TimeTask newTask = new TimeTask(taskID, count, destTime, delayTime, callBack, timeUnit); //寻找要替换的id //寻找任务 bool isReplace = false; for (int i = 0; i < cacheTimeTasks.Count; i++) { if (cacheTimeTasks[i].taskID == id) { cacheTimeTasks[i] = newTask; isReplace = true; break; } } if (!isReplace) { for (int i = 0; i < timeTasks.Count; i++) { if (timeTasks[i].taskID == id) { timeTasks[i] = newTask; isReplace = true; break; } } } return isReplace; } private int GettaskId() { lock (obj) { id += 1; } while (true) { if (id >= int.MaxValue) { id = 0; } bool isUsed = false; //如果包含这个id就改变之 for (int i = 0; i < idList.Count; i++) { if (idList[i] == id) { isUsed = true; break; } } if (!isUsed) { //跳出循环 break; } else { id += 1; } } return id; } #endregion #region 帧任务 private int frame = 0; //执行帧任务 private void ExecuteFrameTask() { //从缓存里里面取出来 if (cacheFrameTasks.Count > 0) { foreach (FrameTask task in cacheFrameTasks) { frameTasks.Add(task); } } cacheFrameTasks.Clear(); frame += 1; for (int i = 0; i < frameTasks.Count; i++) { FrameTask frameTask = frameTasks[i]; if (frame < frameTask.frameTime) { continue; } else { if (frameTask.callBack != null) { frameTask.callBack.Invoke(); } //到达时间了 //判断任务执行的次数 if (frameTask.count == 1) { //移除任务 frameTasks.RemoveAt(i); i--; cacheIdList.Add(frameTask.taskID); break; } else { if (frameTask.count != 0) { frameTask.count -= 1; } //时间加 frameTask.frameTime += frameTask.delayTime; } } } } //添加任务 默认是毫秒 返回添加任务的id public int AddFrameTask(int frameTime, int delayTime, Action callBack, int count = 1) { int taskID = GettaskId(); FrameTask timeTask = new FrameTask(taskID, count, frameTime + frame, delayTime, callBack); //添加到idList里 //添加到缓存里 idList.Add(taskID); cacheFrameTasks.Add(timeTask); return taskID; } //删除任务 public bool DeleteFrameTask(int taskId) { //寻找任务 bool isDelete = false; for (int i = 0; i < cacheFrameTasks.Count; i++) { if (cacheFrameTasks[i].taskID == taskId) { cacheFrameTasks.RemoveAt(i); isDelete = true; break; } } if (!isDelete) { for (int i = 0; i < frameTasks.Count; i++) { if (frameTasks[i].taskID == taskId) { frameTasks.RemoveAt(i); isDelete = true; break; } } } return isDelete; } //替换任务 public bool ReplaceFrameTask(int id, int frameTime, int delayTime, Action callBack, int count = 1) { //构造新任务 int taskID = GettaskId(); FrameTask newFrameTask = new FrameTask(taskID, count, frameTime + frame, delayTime, callBack); //寻找要替换的id //寻找任务 bool isReplace = false; for (int i = 0; i < cacheFrameTasks.Count; i++) { if (cacheFrameTasks[i].taskID == id) { cacheFrameTasks[i] = newFrameTask; isReplace = true; break; } } if (!isReplace) { for (int i = 0; i < frameTasks.Count; i++) { if (frameTasks[i].taskID == id) { frameTasks[i] = newFrameTask; isReplace = true; break; } } } return isReplace; } #endregion}#region 任务类public enum TimeUnit{ MilliSecond, //毫秒 Second, //秒 Minute, //分钟 Hour, //小时 Day, //天}public class TimeTask{ public int taskID;//任务id public int count;//循环次数 public double destTime;//目标时间 public double delayTime;//延迟时间 public Action callBack; //执行的方法 public TimeUnit timeUnit;//时间类型 public TimeTask(int taskID, int count, double destTime, double delayTime, Action callBack, TimeUnit timeUnit) { this.taskID = taskID; this.count = count; this.destTime = destTime; this.delayTime = delayTime; this.callBack = callBack; this.timeUnit = timeUnit; }}/// /// 帧定时任务/// public class FrameTask{ public int taskID;//任务id public int count;//循环次数 public int frameTime;//目标时间 public int delayTime;//延迟时间 public Action callBack; //执行的方法 public FrameTask(int taskID, int count, int frameTime, int delayTime, Action callBack) { this.taskID = taskID; this.count = count; this.frameTime = frameTime; this.delayTime = delayTime; this.callBack = callBack; }}#endregion
/**************************************************** 文件:TimeSys.cs 作者:唐孝辉 邮箱: 1351105506@qq.com 日期:#CreateTime# 功能:定时任务系统*****************************************************/using System;using System.Collections.Generic;using UnityEngine;public class TimeSys : MonoBehaviour{ private PETimer peTimer; public void Init() { peTimer=new PETimer(); peTimer.SetLog((string s)=>{Debug.Log(s);}); peTimer.LogInfo("初始化成功!"); } void Update() { peTimer.Update(); } #region 任务 //添加任务 默认是毫秒 返回添加任务的id public int AddTask( float destTime, float delayTime,Action callBack, TimeUnit timeUnit=TimeUnit.MilliSecond, int count = 1) { return peTimer.AddTask(destTime, delayTime, callBack, timeUnit, count); } //删除任务 public bool DeleteTask(int taskId) { return peTimer.DeleteTask(taskId); } //替换任务 public bool ReplaceTask(int id,float destTime, float delayTime, Action callBack, TimeUnit timeUnit = TimeUnit.MilliSecond, int count = 1) { return peTimer.ReplaceTask(id, destTime, delayTime, callBack, timeUnit, count); } #endregion #region 帧任务 //添加任务 默认是毫秒 返回添加任务的id public int AddFrameTask(int frameTime, int delayTime, Action callBack, int count=1) { return peTimer.AddFrameTask(frameTime, delayTime, callBack,count); } //删除任务 public bool DeleteFrameTask(int taskId) { return peTimer.DeleteFrameTask(taskId); } //替换任务 public bool ReplaceFrameTask(int id, int frameTime, int delayTime, Action callBack, int count= 1) { return peTimer.ReplaceFrameTask(id, frameTime, delayTime, callBack, count); } #endregion}
/**************************************************** 文件:GameRoot.cs 作者:唐孝辉 邮箱: 1351105506@qq.com 日期:#CreateTime# 功能:Nothing*****************************************************/using UnityEngine;public class GameRoot : MonoBehaviour{ private TimeSys timeSys; private int taskID;//当前任务的id private int taskFrameID;//当前帧任务的id void Start() { timeSys = this.GetComponent(); timeSys.Init(); } public void AddTask() { taskID=timeSys.AddTask(1000, 1000, () => { Debug.Log("执行任务:" + taskID); },TimeUnit.MilliSecond,5); } public void DeleteTask() { bool success=timeSys.DeleteTask(taskID); Debug.Log("删除任务id:" + taskID+success); } public void ReplaceTask() { bool success = timeSys.ReplaceTask(taskID,1000, 1000, () => { Debug.Log("执行新的替换的任务!" ); }, TimeUnit.MilliSecond, 5); Debug.Log("替换:" + success); } public void AddFrameTask() { taskFrameID=timeSys.AddFrameTask(60, 60, () => { Debug.Log("执行帧任务:" + taskFrameID); }, 6); } public void DeleteFrameTask() { bool success = timeSys.DeleteFrameTask(taskFrameID); Debug.Log("删除帧任务id:" + taskID + success); } public void ReplaceFrameTask() { bool success = timeSys.ReplaceFrameTask(taskFrameID,60, 60, () => { Debug.Log("执行新的替换的帧任务"); }, 5); Debug.Log("替换:" + success); }}
转载地址:http://vjrxo.baihongyu.com/