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

 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. 2、Hdfs架构设计与原理分析

    文章目录 1.Hadoop架构 2.HDFS体系架构 2.1NameNode 2.1.1元数据信息 2.1.2NameNode文件操作 2.1.3NameNode副本 2.1.4NameNode心跳机 ...

  2. 亚马逊云推出基于机器学习的企业搜索服务Kendra,剑指微软

    近日,在AWS re:Invent全球大会上,亚马逊发布了五项新的基于机器学习的人工智能 (AI) 服务. 这五项服务包括机器学习驱动的企业搜索.代码审核与分析.欺诈检测.医疗转录和 AI 预测的人工 ...

  3. window服务session隔离

    在window服务中抓取窗体是做不到的,因为window系统的session隔离机制:如果想要调用外部程序,可以通过 创建代理进程 进行操作(通过非托管代码CreateProcessAsUser函数进 ...

  4. Python标准库之logging模块

    很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误.警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储各种格式的日志,loggin ...

  5. 小白的java学习之路 “ 类和对象”

    一.※ 万物皆对象 二.对象的两个特征: 属性:对象具有的各种特征 方法:对象执行的操作 对象:用来描述客观事物的一个实体,由一组属性和方法构成 三.封装: 对象同时具有属性和方法两项特性 对象的属性 ...

  6. 二叉堆(1)BinaryHeap

    封装一个简单二叉堆,亦可视为优先队列. 测试文件 main.cpp: #include <iostream> #include "BinaryHeap.h" using ...

  7. CSS 实现一个自适应的正方形

      传统方法正方形用固定的形式写 直接长=宽写固定的值如下   .box{   width: 200px;   height: 200px;   background: pink;   color: ...

  8. 我的第一个原生Web Components——滑块(SingleSlider)

    写着写着,就会跑偏,没错又走上了一个岔道……就是不知道这条岔道以后会不会越来越宽,有的说他是未来,有的说…… 这里不知道,也不做什么评断.减少一些重复性的工作,提高开发效率这是最根本的.说白了就是偷懒 ...

  9. npm命令笔记-----转自网络,仅供自己查看使用

    npm是一个node包管理和分发工具,已经成为了非官方的发布node模块(包)的标准.有了npm,可以很快的找到特定服务要使用的包,进行下载.安装以及管理已经安装 的包. npm常用指令 1.npm ...

  10. prach 839滤波系数