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.团队选题:餐厅到店点餐系 ...
随机推荐
- javascript之常遇到的浏览器兼容问题和解决方法
转自http://www.cnblogs.com/duenyang/p/6066737.html 常遇到的关于浏览器的宽高问题: var winW=document.body.clientWidth| ...
- Start Developing Mac Apps -- Human Interface Design 用户界面设计
Human Interface Design It’s not enough to create an app that works. Users expect Mac apps to be powe ...
- 看鸟哥的Linux私房菜的一些命令自我总结(二)
-关于执行文件路径的变量 $PATH -查看文件与目录 ls -a :全部的文件,连同隐藏文件一起列出来 -d :仅列出目录本身,而不是列出目录内的文件数据 -i :列出inode号码 - ...
- Unity里的人物驱动/换装备/换武器/换衣服/卡通重定位(转)
Unity里的人物驱动/换装备/换武器/换衣服/动画重定位 刚学的过程被这个问题困扰最多. 首先,基本的,大家都知道驱动人物需要骨架.绑骨的Mesh和动画(这三个要是不知道的话就得考虑看看计算机图形学 ...
- Springboot 配置 application.yml 连接MySQL数据库
1.在pom.xml的<dependencies></dependencies>标签中中加入以下依赖 <dependency> <groupId>org ...
- 修改Cloudera Manager 管理机器的IP
原本在3台机器中部署了Cloudera CDH4.8的集群环境,运行状况良好,后来由于机房搬迁,导致那3台机器的ip地址被改变(hostname 没有变化). 再次启动Cloudera-scm-ser ...
- 黑客攻防技术宝典web实战篇:利用信息泄露习题
猫宁!!! 参考链接:http://www.ituring.com.cn/book/885 随书答案. 1. 当探查 SQL 注入漏洞时,如果请求以下 URL:https://wahh-app.com ...
- NOIp2017真题模拟赛 By cellur925
果然我还是最菜的==不接受反驳 (先考了day2喵喵喵) Day2 T1:奶酪 期望得分:100分 实际得分:100分 考察:并查集 思路:这题其实之前做过了==.思路还是比较清晰的,读入时预处理出可 ...
- hdu1829&&poj2492 A Bug's Life 基础种类并查集
把性别相同的虫子放在同一个集合,然后每读入一对虫子号,判断它们在不在同一集合,在则同性别,不在则继续 #include <cstdio> #include <cstring> ...
- Python 基础知识(5)
1:引用 当我们把一个变量给另一个变量赋值的时候,不是把A变量中的值给B一份,而是把A变量中的地址给了B,这就是引用.任何牵扯到等号赋值的地方,统统都是引用. a = 100 b = a id(a) ...