[Unity3D]计时器/Timer
原地址:http://blog.sina.com.cn/s/blog_5b6cb9500101aejs.html
https://github.com/xuzhiping7/Unity3d-Timer
项目中管理计时器太混乱难看了,用好听点的话来说就是代码不优雅。
//Coded by ZhipingXu xuzhiping7@qq.com
//Too simple, so I do not need to explain, just see the code. Help yourself. public class Timer{ //If the Timer is running
private bool b_Tricking; //Current time
private float f_CurTime; //Time to reach
private float f_TriggerTime; //Use delegate to hold the methods
public delegate void EventHandler(); //The trigger event list
public event EventHandler tick; /// <summary>
/// Init
/// </summary>
/// <param name="second">Trigger Time</param>
public Timer(float second)
{
f_CurTime = 0.0f;
f_TriggerTime = second;
} /// <summary>
/// Start Timer
/// </summary>
public void Start()
{
b_Tricking = true;
} /// <summary>
/// Update Time
/// </summary>
public void Update(float deltaTime)
{
if (b_Tricking)
{
f_CurTime += deltaTime; if (f_CurTime > f_TriggerTime)
{
//b_Tricking must set false before tick() , cause if u want to restart in the tick() , b_Tricking would be reset to fasle .
b_Tricking = false;
tick();
}
}
} /// <summary>
/// Stop the Timer
/// </summary>
public void Stop()
{
b_Tricking = false;
} /// <summary>
/// Continue the Timer
/// </summary>
public void Continue()
{
b_Tricking = true;
} /// <summary>
/// Restart the this Timer
/// </summary>
public void Restart()
{
b_Tricking = true;
f_CurTime = 0.0f;
} /// <summary>
/// Change the trigger time in runtime
/// </summary>
/// <param name="second">Trigger Time</param>
public void ResetTriggerTime(float second)
{
f_TriggerTime = second;
}
}
using UnityEngine;
using System.Collections; public class TimerManager : MonoBehaviour
{
Timer test; // Use this for initialization
void Start () {
test = new Timer(3.0f);
test.tick += Test;
test.tick += Test2;
test.Start();
} // Update is called once per frame
void Update () { //If u have many timer
//u also can serval frame call one time to save some performance, but the deltaTime u should calculate youself
//like :(u should define lastTime youself-- float) /*
if(Time.frameCount%5 == 0)
{
delta = Time.time - lastTime;
test.Update(Time.deltaTime);
lastTime = Time.time;
}
*/ test.Update(Time.deltaTime);
} //Some time u may need this to avoid conflict when re-init something , just a tip .
void OnDestory(){
test.tick -= Test;
test.tick -= Test2;
} void Test()
{
Debug.Log("");
} void Test2()
{
Debug.Log("");
}
}
[Unity3D]计时器/Timer的更多相关文章
- (转)[Unity3D]计时器/Timer
http://blog.sina.com.cn/s/blog_5b6cb9500101aejs.html 项目中管理计时器太混乱难看了,用好听点的话来说就是代码不优雅. 想了下就随手简单写了个时间 ...
- 计时器 Timer
计时器 Timer 不多说了,守则.
- C# - 计时器Timer
System.Timers.Timer 服务器计时器,允许指定在应用程序中引发事件的重复时间间隔. using System.Timers: // 在应用程序中生成定期事件 public class ...
- Android中三种计时器Timer、CountDownTimer、handler.postDelayed的使用
在android开发中,我们常常需要用到计时器,倒计时多少秒后再执行相应的功能,下面我就分别来讲讲这三种常用的计时的方法. 一.CountDownTimer 该类是个抽象类,如果要使用这个类中的方法, ...
- 松软科技课堂:索引器计时器Timer
在.NET中有三种计时器:1.System.Windows.Forms命名空间下的Timer控件,它直接继承自Componet.Timer控件只有绑定了Tick事件和设置Enabled=True后才会 ...
- Java计时器Timer和TimerTask用法
package com.sy.game.test; import java.util.Timer; import java.util.TimerTask; public class TimeTask ...
- C# 如何用计时器Timer控件实现停留几秒再做切换窗体的操作
C# Timer用法及实例详解 关于C# Timer类 在C#里关于定时器类就有3个 C# Timer使用的方法1.定义在System.Windows.Forms里 C# Timer使用的方法2.定 ...
- 简单实现一个Unity3d的Timer
数量使用的不太多,没有实现对象池. using System.Collections; using System.Collections.Generic; using UnityEngine; usi ...
- 计时器timer的使用
https://www.cnblogs.com/ILoveSleep/archive/2013/06/12/3133322.html
随机推荐
- 学习之路三十五:Android和WCF通信 - 大数据压缩后传输
最近一直在优化项目的性能,就在前几天找到了一些资料,终于有方案了,那就是压缩数据. 一丶前端和后端的压缩和解压缩流程 二丶优点和缺点 优点:①字符串的压缩率能够达到70%-80%左右 ②字符串数量更少 ...
- AngularJS - 服务简介
服务是AngularJS中非常重要的一个概念,虽然我们有了控制器,但考虑到其生命实在脆弱,我们需要用到服务. 起初用service时,我便把service和factory()理所当然地关联起来了. 确 ...
- searchBar控件
那就先了解一下UISearchBar控件吧! UISearchBar控件就是要为你完成搜索功能的一个专用控件.它集成了很多你意想不到的功能和特点! 首先,还是来普及一下UISearchBar控件API ...
- G-nav-02
/*header: Navigation public style*/header:before, header:after ,.navigation:before, .navigation:afte ...
- ncdu 磁盘目录查看工具
我平时都是直接yum -y install ncdu,但是今天失败了. 所以: 安装EPEL源 CentOS/RHEL 5 : rpm -Uvh https://dl.fedoraproject.or ...
- iOS开发小技巧--设置cell左右有空隙,设置分割线的新思路,重写setFrame:让别人在外界无法修改控件的大小
如图:需要自定义cell
- requirejs
//index.html <!doctype html> <html> <head> <meta charset="utf-8"> ...
- Java设计模式-中介者模式(Mediator)
中介者模式也是用来降低类类之间的耦合的,因为如果类类之间有依赖关系的话,不利于功能的拓展和维护,因为只要修改一个对象,其它关联的对象都得进行修改.如果使用中介者模式,只需关心和Mediator类的关系 ...
- Shell编程中Shift的用法
Shell编程中Shift的用法 位置参数可以用shift命令左移.比如shift 3表示原来的$4现在变成$1,原来的$5现在变成$2等等,原来的$1.$2.$3丢弃,$0不移动.不带参数的shif ...
- MySQL tips (日期时间操作/concat 等)
1. Query结尾要加一个分号: 2. 数据库和表 SHOW DATABASES; USE YOUR_DB; SHOW TABLES; SHOW COLUMNS FROM study或者D ...