Unity计时器

12 篇文章 0 订阅

话不多说直接上代码

 

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
}

 


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


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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值