[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
随机推荐
- jqurey 遍历 div内的所有input单选复选按钮并判断是否选中及Attr(checked)无效的解决
关于页面前面标签 <ul> @{ foreach (var item in vote) { if (!string.IsNullOrEmpty(item.Img)) { <li cl ...
- [BZOJ 1797][AHOI2009]最小割(最小割关键边的判断)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1797 分析: 在残余网络中看: 对于第1问: 1.首先这个边必须是满流 2.其次这个边 ...
- Linq之Linq to Objects
目录 写在前面 系列文章 linq to objects 总结 写在前面 上篇文章介绍了linq的延迟加载特性的相关内容,从这篇文章开始将陆续介绍linq to Objects,linq to xml ...
- WCF 入门 (18)
前言 感冒了呀...but 不忌油炸,不忌辛辣,o(∩_∩)o . 第18集 WCF服务应该抛出fault 异常 Throwing fault exceptions from a WCF servic ...
- javascript 重难点(原型链 this) 理解总有一个过程,不要急,循序渐进!
开始补充: 1. 将函数定义作为对象的属性,称之为对象方法.2. this的指向是由它所在函数调用的上下文决定的(语境),而不是由它所在函数定义的上下文决定的.3. 因为当一个函数作为函数而不是方法来 ...
- js获取服务器时间戳
<!DOCTYPE html> <html> <head> <title>ajax</title> </head> <bo ...
- maven中Rhino classes (js.jar) not found - Javascript disabled的处理
想使用单元测试 来测一下服务请求,于是想到了使用Junit,查了一下,决定使用 HttpUnit 来发送请求 于是在maven中引入了 <dependency> <groupId&g ...
- Spring-如何实现事物管理的
事务的实现方式 实现方式共有两种:编码方式:声明式事务管理方式.基于AOP技术实现的声明式事务管理,实质就是:在方法执行前后进行拦截,然后在目标方法开始之前创建并加入事务,执行完目标方法后根据执行情况 ...
- 关于OOM那些事儿
最近在aliyun上crontab里放的一个java脚本把机器搞翻了,ssh连不上T_T 发现OOM了,真是无语.并不懂Java的内存模型,转一篇备用吧. 转载自:http://www.cnblogs ...
- .net 代码风格规范
声明:内容非原创,转自张子阳博客. 对于为什么是转载,唯一原因就是这东西居然比我整理的好,直接用得了. 1. C# 代码风格要求 1.1注释 类型.属性.事件.方法.方法参数,根据需要添加注释. 如果 ...