本文共 3027 字,大约阅读时间需要 10 分钟。
/**************************************************** 文件:TimeTask.cs 作者:唐孝辉 邮箱: 1351105506@qq.com 日期:#CreateTime# 功能:任务类*****************************************************/using System;////// 时间类型/// 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 作者:唐孝辉 邮箱: 1351105506@qq.com 日期:#CreateTime# 功能:定时系统*****************************************************/using System;using System.Collections.Generic;using System.Threading;using UnityEngine;public class TimerSys : MonoBehaviour{ private ListcacheTaskList=new List (); //缓存 private List taskList=new List (); private int taskid = 0; private static readonly string obj = "22"; private List idList=new List (); 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 = 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
/**************************************************** 文件:GameRoot.cs 作者:唐孝辉 邮箱: 1351105506@qq.com 日期:#CreateTime# 功能:GameRoot*****************************************************/using System.Collections.Generic;using UnityEngine;public class GameRoot : MonoBehaviour{ private TimerSys timerSys; private int taskId; void Start() { timerSys = this.GetComponent(); } //开始任务 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); }}
转载地址:http://sjrxo.baihongyu.com/