简单实现一个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 ...
随机推荐
- 环绕通知(xml)
1.maven依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...
- html font后面跟多种字体
例子 body,div,dl,dt,dd,li,h1,h2,h6,pre,form,fieldset,input,textarea,p,blockquote,th,td { margin: 0; pa ...
- STL-queue 队列
#include <iostream> #include <queue> using namespace std; int main() { // queue也很简单 // p ...
- webpack4从安装到使用入过的那些坑
1.全局安装 安装 webpack 前要确保已经下载 node.js 最新版本(node.js官网地址) 目前使用命令行安装的 webpack 默认为最新版本(以下我使用了全局安装.官网建议本地安装, ...
- .net 基础汇总(1)
1 转载 String.IsNullOrWhiteSpace和String.IsNullOrEmpty的区别
- Secondary NameNode:它究竟有什么作用?
前言 最近刚接触Hadoop, 一直没有弄明白NameNode和Secondary NameNode的区别和关系.很多人都认为,Secondary NameNode是NameNode的备份,是为了防止 ...
- ASP.NET常用内置对象(二)Response
response翻译为中文:响应. 将数据作为请求的结果从服务器发送到客户浏览器中,并提供有关响应的消息.它可用来在页面中输出数据,在页面中跳转,还可以传递各个页面的参数. Response对象是Sy ...
- CTS、CLS、CLR
CTS.CLS和CLR是.NET框架的3个核心部分,下面分别对它们进行介绍. 1)CTS Common Type System CTS即通用类型系统,它定义了如何在.NET Framework运行库 ...
- Power Strings[poj2406]题解
Power Strings Description - Given two strings a and b we define ab to be their concatenation. For ex ...
- jQuery---城市选择案例
城市选择案例 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UT ...