简单实现一个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 ...
随机推荐
- netty 的事件驱动
netty 是事件驱动的,这里面有两个含义,一是 netty 接收到 socket 数据后,会产生事件,事件在 pipeline 上传播,二是事件由特定的线程池处理. NioEventLoop 轮询网 ...
- 转: Laravel的数据库迁移 介绍的比较清晰
原文: https://blog.sbot.io/articles/12/Laravel-数据库迁移(Database-Migrations)操作实例 很多人可能在学习Laravel框架的时候,对La ...
- Centos 7 配置 NFS
安装NFS包 yum install nfs-utils.x86_64 启动NFS服务需要首先启动rpcbind服务,这个rpcbind包已经在上面安装好了 先配置 /etc/exports 文件 v ...
- .NET/C# 万能 HTTP 模拟请求框架
我是一名 ASP.NET 程序员,专注于 B/S 项目开发.累计文章阅读量超过一千万,我的博客主页地址:https://www.itsvse.com/blog_xzz.html HttpHelper ...
- seleniumChrom无头浏览器
---------------------- 谷歌无头浏览器 ----------------------------- import time from selenium import webdri ...
- HDU1862 - EXCEL排序
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1862 解题思路:结构体排序 #include <bits/stdc++.h> using ...
- LOJ #6402. yww 与校门外的树 多项式求逆
蛮神的一道题. code: #include <cmath> #include <cstring> #include <algorithm> #include &l ...
- so the first day
welcome to the world it sucks but you gona love it
- CF1254E Send Tree to Charlie
题意 讲不太清楚,看英文吧 cf 做法 在正式开始之前,我们先来玩一玩性质 首先考虑全\(0\)的情况,即本质不同的方案数 性质1:方案数并不为(n-1)!,即方案与结果不为双射 考虑一条边将树分为两 ...
- c语言中 char* 和 unsigned char* 的区别浅析(转)
原文:https://blog.csdn.net/guotianqing/article/details/77341657 背景最近在项目中遇到了一个编译警告,是因为定义的变量为char[],而在使用 ...