您的位置:首页 > 移动开发 > Unity3D

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);
}
}
}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: