• 数量使用的不太多,没有实现对象池.

 using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events; public class ZSTimerManager: SingletonGeneric<ZSTimerManager>
{
private List<ZSTimerTask> timers = new List<ZSTimerTask>();
public static ZSTimerManagerStub stub = new GameObject("ZSTimerManagerStub").AddComponent<ZSTimerManagerStub>(); public ZSTimerTask Create(int total, int interval, int ID)
{
var t = timers.Find(pt => pt.ID == ID);
if (t != null)
{
Debug.LogErrorFormat("the timer what ID = {0} already exist.", ID);
return null;
}
ZSTimerTask task = new ZSTimerTask(ID);
timers.Add(task); return task;
} public ZSTimerTask GetTimer(int ID)
{
return timers.Find(task => task.ID == ID);
} public void RemoveTimer(int ID)
{
var timer = GetTimer(ID);
if (timer == null) Debug.LogErrorFormat("ID = {0} are not found.", ID);
timers.Remove(GetTimer(ID));
}
} public class ZSTimerTask
{
/// <summary>
/// 总时间
/// </summary>
private int _total;
/// <summary>
/// 间隔时间
/// </summary>
private int _interval;
/// <summary>
/// ID
/// </summary>
public int ID { get; set; } private Coroutine driver;
/// <summary>
/// 剩余时间
/// </summary>
public int Therest { get; private set; } public UnityEvent OnStart = new UnityEvent();
public UnityEvent OnComplete = new UnityEvent();
public UnityEvent OnInterval = new UnityEvent(); public ZSTimerTask(int id)
{
this.ID = id;
} public virtual void Start(int total, int interval)
{
_total = total;
Therest = _total;
_interval = interval;
OnStart.Invoke();
driver = ZSTimerManager.stub.StartCoroutine(TickRoutine());
} public virtual void Stop()
{
if (driver != null) ZSTimerManager.stub.StopCoroutine(driver);
} public virtual void Reset()
{
Therest = _total;
if (driver != null) ZSTimerManager.stub.StopCoroutine(TickRoutine());
driver = ZSTimerManager.stub.StartCoroutine(TickRoutine());
} /// <summary>
/// DRIVER
/// </summary>
protected virtual IEnumerator TickRoutine()
{
do
{
OnInterval.Invoke();
yield return new WaitForSeconds(_interval);
Therest -= _interval;
} while (Therest >= );
OnComplete.Invoke();
} public virtual void Close()
{
OnStart.RemoveAllListeners();
OnComplete.RemoveAllListeners();
OnInterval.RemoveAllListeners();
if (driver != null) ZSTimerManager.stub.StopCoroutine(TickRoutine());
}
} public sealed class ZSTimerManagerStub : MonoBehaviour { }

Smart Timers Manager

简单实现一个Unity3d的Timer的更多相关文章

  1. 最简单的一个Oracle定时任务

    最简单的一个Oracle定时任务一.在PLSQL中创建表:create table HWQY.TEST(CARNO     VARCHAR2(30),CARINFOID NUMBER) 二.在PLSQ ...

  2. 在浏览器中简单输入一个网址,解密其后发生的一切(http请求的详细过程)

    在浏览器中简单输入一个网址,解密其后发生的一切(http请求的详细过程) 原文链接:http://www.360doc.com/content/14/1117/10/16948208_42571794 ...

  3. shell中,我们可以通过简单的一个判断来判断命令是否存在

    shell中,我们可以通过简单的一个判断来判断命令是否存在 which "Command" > /dev/null if [ $? -eq 0 ] then echo com ...

  4. 编写函数求整形数组a中存储的m个不重复的整数的第k大的整数(其中m>=1,1<=k<=m)很简单的一个思路是酱紫的:管他辣么多干啥,上来一把排序然后直接得答案

    /** * @author:(LiberHome) * @date:Created in 2019/2/28 20:38 * @description: * @version:$ *//*编写函数求整 ...

  5. Blender简单动画:一个小球从一座山上滚下.

    简单动画:一个小球从一座山上滚下.注:[key]方括号内是快捷键; {大括号}内是模式,页签名称或选项等. ==== 1. 建模:    == 1.1 山[shift A] 建立平面plane,可以大 ...

  6. [k8s]简单启动一个k8s集群

    简单启动一个k8s集群 kube-master mkdir -p /root/logs/api-audit /root/logs/controller /root/logs/scheduler kub ...

  7. Vue - 简单实现一个命令式弹窗组件

    前言 在日常工作中弹窗组件是很常用的组件,但用得多还是别人的,空闲时间就自己来简单实现一个弹窗组件 涉及知识点:extend.$mount.$el 使用方式: this.$Confirm({ titl ...

  8. 一个简单的一个sql表遍历

    简单的一个sql表遍历 一般我们写储存过程或者其他sql语句的时候都会用到循环遍历数据,最常用的两种就是 1.游标 2.临时表+while 下面贴出示例代码 DECLARE @MinReLogID I ...

  9. MY SQL数据库密码最简单的一个方法()

    https://zhidao.baidu.com/question/564368111.html 非常简单的一个修改方法!!!!!!!!!!!!!!!!!!!!! 最简单的方法就是借助第三方工具Nav ...

随机推荐

  1. 【剑指Offer】61、把二叉树打印成多行

    题目描述 从上到下按层打印二叉树,同一层结点从左至右输出.每一层输出一行. 题解一:BFS public static ArrayList<ArrayList<Integer>> ...

  2. hdu 1011 Starship Troopers(树上背包)

    Problem Description You, the leader of Starship Troopers, are sent to destroy a base of the bugs. Th ...

  3. magento2.2.3 根据产品ID获取栏目名称的正确调用方式

    根据product_id 获取 category_ids : /** * @param $product_id * @return array */ public function mc_getCat ...

  4. Centos 7 安装配置git

    Centos 7 安装配置git 1.安装git yum install git 2.验证git git -version 3.配置基本信息

  5. Spark性能优化指南——初级篇

    原文来我的公众号:Spark性能优化指南——初级篇 一. Spark作业原理 我们使用spark-submit提交一个Spark作业之后,这个作业就会启动一个对应的Driver进程.该进程是向集群管理 ...

  6. pyecharts学习笔记2

    目录 line bar grid overlap tap 这个画图是真的美观.香嘛? line 普通折线图 bar 柱状图 grid 可以让不同类型的图展示到同一个画面上 overlap 叠加 tap ...

  7. Codeforces Round #592 (Div. 2) D - Paint the Tree

    题目链接:https://codeforces.com/contest/1244/problem/D 题意:给你一个树,让你把树上的每个节点染成三种颜色,使得任意三个互相相邻的节点颜色都不一样(意思是 ...

  8. kindle怎么导入电子书

    参考网址:https://jingyan.baidu.com/article/59a015e342a165f795886545.html

  9. C++ lvalue(左值)和rvalue(右值)

    lvalue(左值)和rvalue(右值) 昨天写代码遇见一个这样的错误:{ "cannot bind non-const lvalue reference of type 'int& ...

  10. ubuntu 16.04.1上安装并使用nginx

    1.安装 sudo apt-get install nginx 2.这时候应该就已经启动了,跳到步骤4. 通过命令“ps -aux | grep nginx”看下是否启动 3.如果没有启动,可以使用命 ...