Unity计时器
2019-05-18 10:26
127 查看
话不多说直接上代码
[code]using System; /// <summary> /// 存储计时器属性 /// </summary> public class PETimeTaskZty { public float destTime;//毫秒 public Action callback;//回调 public int count;//执行次数 public float delay;//延时时间 public int tid;//用来识别的id } /// <summary> /// 时间模式 /// </summary> public enum PETimeUnit { Milisecond, Second, Minute, Hour, Day }
[code]using System; using System.Collections.Generic; using UnityEngine; public class TimerSystemZty : MonoBehaviour { public static TimerSystemZty instance; private void Awake() { if (instance == null) { instance = this; } } private static readonly string obj = "lock"; private int tid; private List<int> tidList = new List<int>();//Tid列表 private List<int> recTidList = new List<int>();//回收Tid用到的缓存列表 private List<PETimeTaskZty> tempTimeList = new List<PETimeTaskZty>();//缓存列表 private List<PETimeTaskZty> taskTimeList = new List<PETimeTaskZty>();//计时器列表 private void Update() { //加入缓存区中的定时任务 for (int i = 0; i < tempTimeList.Count; i++) { taskTimeList.Add(tempTimeList[i]); } tempTimeList.Clear(); //遍历检测任务是否到达条件 for (int i = 0; i < taskTimeList.Count; i++) { PETimeTaskZty task = taskTimeList[i]; if (task.destTime > Time.realtimeSinceStartup*1000) { continue; } else { Action cb = task.callback; if (cb != null) { cb(); } //移除已经完成的任务 if (task.count == 1) { taskTimeList.RemoveAt(i); i--; recTidList.Add(task.tid); } else { if (task.count != 0) { task.count -= 1; } task.destTime += task.delay; } } } //清理多余计时器 if (recTidList.Count > 0) { RectcleTid(); } } /// <summary> /// 添加一个计时器 /// </summary> /// <param name="callback"></param> /// <param name="delay"></param> /// <param name="timeUnit"></param> /// <param name="count"></param> /// <returns></returns> public int AddTimeTask(Action callback, float delay, PETimeUnit timeUnit = PETimeUnit.Milisecond, int count = 1) { if (timeUnit != PETimeUnit.Milisecond) { switch (timeUnit) { case PETimeUnit.Second: delay = delay * 1000; break; case PETimeUnit.Minute: delay = delay * 1000 * 60; break; case PETimeUnit.Hour: delay = delay * 1000 * 60 * 60; break; case PETimeUnit.Day: delay = delay * 1000 * 60 * 60 * 24; break; default: break; } } int tid = GetTid(); float destTime = Time.realtimeSinceStartup * 1000 + delay; PETimeTaskZty timeTask = new PETimeTaskZty(); timeTask.destTime = destTime; timeTask.callback = callback; timeTask.count = count; timeTask.delay = delay; timeTask.tid = tid; tempTimeList.Add(timeTask); tidList.Add(tid); return tid; } /// <summary> /// 得到Tid /// </summary> /// <returns></returns> private int GetTid() { lock (obj) { tid += 1; while (true) { if(tid == int.MaxValue) { tid = 0; } bool used = false; for (int i = 0; i < tidList.Count; i++) { if (tid == tidList[i]) { used = true; break; } } if (!used) { break; } else { tid += 1; } } } return tid; } /// <summary> /// 删除一个计时器 /// </summary> /// <param name="tid"></param> /// <returns></returns> public bool DeleteTimeTask(int tid) { bool exist = false; for (int i = 0; i < taskTimeList.Count; i++) { PETimeTaskZty task = taskTimeList[i]; if (task.tid == tid) { taskTimeList.RemoveAt(i); for (int j = 0; j < tidList.Count; j++) { if (tidList[j] == tid) { tidList.RemoveAt(j); break; } } exist = true; break; } } if (!exist) { for (int i = 0; i < tempTimeList.Count; i++) { PETimeTaskZty task = tempTimeList[i]; if(task.tid == tid) { tempTimeList.RemoveAt(i); for (int j = 0; j < tidList.Count; j++) { if (tidList[j] == tid) { tidList.RemoveAt(j); break; } } exist = true; break; } } } return exist; } /// <summary> /// 回收Tid /// </summary> public void RectcleTid() { for (int i = 0; i < recTidList.Count; i++) { int tid = recTidList[i]; for (int j = 0; j < tidList.Count; j++) { if(tid == tidList[j]) { tidList.RemoveAt(j); break; } } } recTidList.Clear(); } }
[code]using UnityEngine; /// <summary> /// 测试脚本 /// </summary> public class Test : MonoBehaviour { private int tid; // Use this for initialization void Start () { tid = TimerSystemZty.instance.AddTimeTask(()=>{ Debug.Log(tid); },500,PETimeUnit.Milisecond,0); } private void Update() { if (Input.GetKeyDown(KeyCode.A)) { TimerSystemZty.instance.DeleteTimeTask(tid); } } }
相关文章推荐
- Unity的几中计时器
- unity 计时器
- 【Unity】计时器
- Unity 模拟协程实现计时器功能
- unity 计时器
- [Unity][计时器][协程]协程计时器倒计时
- Unity_计时器的实现
- unity计时器功能的实现
- 面向组件编程之Unity 9.计时器 关键字:Time,Time.deltatime
- Unity计时器设计 - 基础计时器
- Unity长按屏幕计时器
- Unity 计时器
- Unity 封装 倒计时(计时器,CountDownTimer),实现周期更新、技能冷却等功能
- Unity-只使用一个计时器来实现多波怪的生成
- Unity 计时器
- [Unity] 使用Profiler.BeginSample()定位性能热点
- Unity 关于资源加载的时间消耗
- Unity VR简单操作原理方法
- 线程的优先级,停让(yiead()),wait,notify,notifyAll等待与叫醒,线城池,计时器
- 小解Unity如何抽换AC?