Unity3D——Epirome框架_TimerManager计时任务管理器
1、Timer timer = new Timer(); 创建时间管理器 参数(float time, TimeUnit timeUnit,bool ignoreTimeScale = false, bool autoStart = true) time:时间值、timeUnit 时间单位(帧率、秒、厘秒、毫秒)、ignoreTimeScale 忽略时间缩放(可以使游戏暂停更容易实现)、autoStart 自动开始计时
2、timer.loop = true; // 循环 timer.Finished +=method; // 回调函数 timer.Pause(); // 暂停
using System.Collections.Generic;
using UnityEngine;
namespace Epitome
{
public delegate void FinishedHandler();
public class Timer
{
TimerManager.TimerState timer;
public bool Running { get { return timer.Running; } }
public bool Paused { get { return timer.Paused; } }
public bool Loop
{
set { timer.HasRepeat = value; }
get { return timer.HasRepeat; }
}
public event FinishedHandler Finished;
public Timer(float time, TimeUnit timeUnit,bool ignoreTimeScale = false, bool autoStart = true)
{
timer = TimerManager.Instance.CreateTimer(time, timeUnit, ignoreTimeScale);
timer.Finished += TimerFinished;
if (autoStart) timer.Start();
}
public void Start() { timer.Start(); }
public void Stop() { timer.Stop(); }
public void Pause() { timer.Pause(); }
public void UnPause() { timer.UnPause(); }
public void TimerFinished()
{
FinishedHandler handler = Finished;
if (handler != null)
handler();
}
}
public enum TimeUnit
{
FrameRate, // 帧率
Second, // 秒
CentiSecond, // 厘秒:是一秒的百分之一(0.01秒)
MilliSecond, // 毫秒:是一秒的千分之一(0.001秒)
}
public class TimerManager : MonoSingleton<TimerManager>
{
public class TimerState
{
bool running;
bool paused;
bool stopped;
public bool Running { get { return running; } }
public bool Paused { get { return paused; } }
public event FinishedHandler Finished;
private TimeUnit timeUnit;
private float delayTime; // 延迟时间
private float attackTime; // 启动时间
private float currentTime; // 当前时间
public bool HasRepeat; // 一直重复
public bool ignoreTimeScale { get; private set; } // 忽略时间缩放
public TimerState(float time, TimeUnit unit, bool ignore)
{
timeUnit = unit;
ignoreTimeScale = ignore;
delayTime = time;
ResetState();
}
private void ResetState()
{
switch (timeUnit)
{
case TimeUnit.FrameRate:
currentTime = 0.0f;
break;
case TimeUnit.Second:
case TimeUnit.CentiSecond:
case TimeUnit.MilliSecond:
if (!ignoreTimeScale) currentTime = 0.0f;
else currentTime = Time.realtimeSinceStartup;
break;
}
attackTime = delayTime + currentTime;
}
public void UpdateTime(float time)
{
time = ignoreTimeScale ? time - currentTime : time;
if (running)
{
if (paused) return;
switch (timeUnit)
{
case TimeUnit.FrameRate:
currentTime += 1;
break;
case TimeUnit.Second:
currentTime += time;
break;
case TimeUnit.CentiSecond:
currentTime += time * 100;
break;
case TimeUnit.MilliSecond:
currentTime += time * 1000;
break;
}
if (currentTime >= attackTime)
{
if (HasRepeat)
{
ResetState();
}
else
{
Stop();
}
FinishedHandler handle = Finished;
if (handle != null)
{
handle();
}
}
}
}
public void Start()
{
running = true;
}
public void Stop()
{
stopped = true;
running = false;
}
public void Pause()
{
paused = true;
}
public void UnPause()
{
paused = false;
}
}
private List<TimerState> timerList = new List<TimerState>();
private void Update()
{
for (int i = 0; i < timerList.Count ; i++)
{
timerList[i].UpdateTime(timerList[i].ignoreTimeScale ? Time.realtimeSinceStartup : Time.deltaTime);
}
}
public TimerState CreateTimer(float time, TimeUnit timeUnit,bool ignoreTimeScale)
{
TimerState newTimer = new TimerState(time, timeUnit, ignoreTimeScale);
timerList.Add(newTimer);
return newTimer;
}
public void ClearTimer() { }
public void ClearAllTimer() { }
}
}
使用案例
public class text : MonoBehaviour {
// Use this for initialization
void Start () {
Time.timeScale = 3;
Timer timer = new Timer(1, TimeUnit.Second); //第三个参数是否忽略时间缩放带来的影响
timer.Loop = true; // 设置可循环
timer.Finished += rw;
}
private void rw()
{
Debug.Log("你好");
}
}
---------------------
Unity3D——Epirome框架_TimerManager计时任务管理器的更多相关文章
- unity3D客户端框架
unity3D客户端框架 博客
- Unity3D知识框架
美术部分: 3d模型,材质,纹理,shader,Animator,Animation,天空盒,灯光效果,烘焙 程序部分: 基本组成: ...
- Unity3d + PureMVC框架搭建
0.流程:LoginView-SendNotification()---->LoginCommand--Execute()--->调用proxy中的函数操作模型数据--LoginProxy ...
- NancyFx框架之检测任务管理器
先建一个空的项目和之前的NancyFx系列一样的步骤 然后建三个文件夹Models,Module,Views 然后分别安装一下组件 jQuery Microsoft.AspNet.SignalR Mi ...
- [Unity3D]Unity资料大全免费分享
都是网上找的连七八糟的资料了,整理好分享的,有学习资料,视频,源码,插件……等等 东西比较多,不是所有的都是你需要的,可以按 ctrl+F 来搜索你要的东西,如果有广告,不用理会,关掉就可以了,如 ...
- [课程设计]Scrum 1.4 多鱼点餐系统开发进度(点餐页面框架布置)
Scrum 1.4 多鱼点餐系统开发进度 (点餐页面框架布置) 1.团队名称:重案组 2.团队目标:长期经营,积累客户充分准备,伺机而行 3.团队口号:矢志不渝,追求完美 4.团队选题:餐厅到店点餐系 ...
- Unity3D编程学习分享
学习地址:http://www.ixueyun.com/lessons/detail-lessonId-692.html 一.课程概述: 以前大部分3D游戏出现在pc和ps.XBox等专业游戏主机上, ...
- 《开源框架那些事儿22》:UI框架设计实战
UI是User Interface的缩写.通常被觉得是MVC中View的部分,作用是提供跟人机交互的可视化操作界面. MVC中Model提供内容给UI进行渲染,用户通过UI框架产生响应,一般而言会由控 ...
- [课程设计]Scrum 1.4 多鱼点餐系统开发进度
Scrum 1.4 多鱼点餐系统开发进度 (点餐页面框架布置) 1.团队名称:重案组 2.团队目标:长期经营,积累客户充分准备,伺机而行 3.团队口号:矢志不渝,追求完美 4.团队选题:餐厅到店点餐系 ...
随机推荐
- NOIP2009 靶型数独
传送门 这道题比我想象之中要暴力一些. 自己一开始有一份写9*9数独的代码,自己试了一下直接交上去只有40分.看来这样是肯定不行的.考虑优化,想在中间贪贪心啥的,但是难以保证正确性.最后学了一招,从数 ...
- 最浅谈的SG函数
[更新] Nim游戏的经验: 每次最多取m个——%(m+1) 阶梯nim——奇数位无视,看偶数位互相独立,成一堆一堆的石子 . . . . 既然被征召去汇总算法..那么挑个简单点的SG函数好了.. 介 ...
- Top的VIRT是什么
Top命令监控某个进程的资源占有情况 下面是各种内存: VIRT:virtual memory usage 1.进程“需要的”虚拟内存大小,包括进程使用的库.代码.数据等 2.假如进程申请1 ...
- rtmplib rtmp协议过程分析
转自:http://chenzhenianqing.cn/articles/1009.html 写的很好,收藏如下,向作者致敬! 没事碰到了librtmp库,这个库是ffmpeg的依赖库,用来接收,发 ...
- eclipse+PyDev里面import win32api报错的问题解决
windows下面eclipse+PyDev的开发环境,安装了pywin32,写import win32api时老提示错误,在idle里正常执行. 原来是安装python库时,python安装路径下面 ...
- 009--test命令和grep作业及Sed作业awk作业和循环结构
一.test命令 -d :目录 test -d /boot -s :文件长度 > 0.非空 test - ...
- 模板 - 数学 - 快速傅里叶变换/快速数论变换(FFT/NTT)
先看看. 通常模数常见的有998244353,1004535809,469762049,这几个的原根都是3.所求的项数还不能超过2的23次方(因为998244353的分解). 感觉没啥用. #incl ...
- 基础BFS+DFS poj3083
//满基础的一道题 //最短路径肯定是BFS. //然后靠右,靠左,就DFS啦 //根据前一个状态推出下一个状态,举靠左的例子,如果一开始是上的话,那么他的接下来依次就是 左,上 , 右 , 下 // ...
- bzoj 2806: [Ctsc2012]Cheat【广义SAM+二分+dp+单调队列】
把模板串建一个广义SAM 然后在线查询,每次在SAM上预处理出一个a[i]表示i位置向前最多能匹配多长的模板串 二分答案L,dp判断,设f[i]为·~i有几个匹配,转移显然是f[i]=max{f[i- ...
- 蒟蒻ACMer回忆录 · 一段弱校ACM的奋斗史
三年半的ACM生涯终于迎来了终点,退役之时,感慨万分,故写此文以纪念逝去的时光,那些为ACM拼搏的日子,那段弱校ACM的奋斗史. 三年半的ACM生涯,窝见证了CUMT从打铁到铜牌的突破,又见证了从铜牌 ...