简单实现一个Unity3d的Timer
- 数量使用的不太多,没有实现对象池.
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 { }
简单实现一个Unity3d的Timer的更多相关文章
- 最简单的一个Oracle定时任务
最简单的一个Oracle定时任务一.在PLSQL中创建表:create table HWQY.TEST(CARNO VARCHAR2(30),CARINFOID NUMBER) 二.在PLSQ ...
- 在浏览器中简单输入一个网址,解密其后发生的一切(http请求的详细过程)
在浏览器中简单输入一个网址,解密其后发生的一切(http请求的详细过程) 原文链接:http://www.360doc.com/content/14/1117/10/16948208_42571794 ...
- shell中,我们可以通过简单的一个判断来判断命令是否存在
shell中,我们可以通过简单的一个判断来判断命令是否存在 which "Command" > /dev/null if [ $? -eq 0 ] then echo com ...
- 编写函数求整形数组a中存储的m个不重复的整数的第k大的整数(其中m>=1,1<=k<=m)很简单的一个思路是酱紫的:管他辣么多干啥,上来一把排序然后直接得答案
/** * @author:(LiberHome) * @date:Created in 2019/2/28 20:38 * @description: * @version:$ *//*编写函数求整 ...
- Blender简单动画:一个小球从一座山上滚下.
简单动画:一个小球从一座山上滚下.注:[key]方括号内是快捷键; {大括号}内是模式,页签名称或选项等. ==== 1. 建模: == 1.1 山[shift A] 建立平面plane,可以大 ...
- [k8s]简单启动一个k8s集群
简单启动一个k8s集群 kube-master mkdir -p /root/logs/api-audit /root/logs/controller /root/logs/scheduler kub ...
- Vue - 简单实现一个命令式弹窗组件
前言 在日常工作中弹窗组件是很常用的组件,但用得多还是别人的,空闲时间就自己来简单实现一个弹窗组件 涉及知识点:extend.$mount.$el 使用方式: this.$Confirm({ titl ...
- 一个简单的一个sql表遍历
简单的一个sql表遍历 一般我们写储存过程或者其他sql语句的时候都会用到循环遍历数据,最常用的两种就是 1.游标 2.临时表+while 下面贴出示例代码 DECLARE @MinReLogID I ...
- MY SQL数据库密码最简单的一个方法()
https://zhidao.baidu.com/question/564368111.html 非常简单的一个修改方法!!!!!!!!!!!!!!!!!!!!! 最简单的方法就是借助第三方工具Nav ...
随机推荐
- 【终端命令】SSH服务,远程登录
一.SSH协议 在Linux中SSH是非常常用的工具,通过SSH客户端我们可以连接到运行了SSH服务器的远程机器上. SSH客户端是一种 使用"Secure Shell (SSH)" ...
- Java-跳跃路线
题目: 小明参加了学校的趣味运动会,其中的一个项目是:跳格子.地上画着一些格子,每个格子里写一个字,如下所示: 从我做起振我做起振兴做起振兴中起振兴中华 比赛时,先站在左上角的写着“从”字的格子里,可 ...
- Net Core使用Lucene.Net和盘古分词器 实现全文检索
Lucene.net Lucene.net是Lucene的.net移植版本,是一个开源的全文检索引擎开发包,即它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎, ...
- Spark学习之路 (六)Spark Transformation和Action[转]
Transformation算子 基本的初始化 (1)java static SparkConf conf = null; static JavaSparkContext sc = null; sta ...
- ElementUI的Table表格添加自定义头CheckBox多选框
在ElmentUI的Table表格组件中,也许你会使用type为selection值的多选框功能,但是此时设置的label属性不生效,不能设置标题名称:有时候我们的需求就是要添加标题名称,那该如何处理 ...
- Linux下快速删除大量小文件引起的磁盘inode(目录索引)过满
1)首先建立一个空白文件夹. mkdir /tmp/empty 然后安装下rsync yum install -y rsync 2)之后使用以下语句即可快速的删除文件. rsync --delete- ...
- JavaDay10(下)
生产者消费者问题 问题描述 有两个进程:一组生产者进程和一组消费者进程共享一个初始为空.固定大小为n的缓存(缓冲区).生产者的工作是制造一段数据,只有缓冲区没满时,生产者才能把消息放入到缓冲区,否则必 ...
- Swaps and Inversions HDU - 6318 树状数组+离散化
#include<iostream> #include<algorithm> #include<cstring> #include<cstdio> us ...
- PP: Deep clustering based on a mixture of autoencoders
Problem: clustering A clustering network transforms the data into another space and then selects one ...
- VSCode配置之open-with-Live-Server 无法打开浏览器【解决方法】
如果你的vscode编辑器打开浏览器时默认打开的是iE,想要把它改为chrome,怎么办呢? 我遇到如下原因: 这是按照网上的setting.json配置 这是运行了 open-with-live-s ...