[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
随机推荐
- DateTime类常用技巧摘录
//今天 DateTime.Now.Date.ToShortDateString(); //昨天,就是今天的日期减一 DateTime.Now.AddDays(-).ToShortDateString ...
- linux中的帮助命令
关键字 man --help help 1.man (1)查看命令 man 命令用来查看别的命令的信息和用法,如man ls表示查看ls的介绍以及用法: (2)查看配置文件的帮助文档 linux下的配 ...
- 编写高质量代码改善C#程序的157个建议[4-9]
前言 本文首先亦同步到http://www.cnblogs.com/aehyok/p/3624579.html.本文主要来学习记录一下内容: 建议4.TryParse比Parse好 建议5.使用int ...
- AngularJS - 路由入门
我们有很多方法让一个视图随着用户的操作进行变化. 但是,只是单单一个视图就要满足所有的需求会让代码变得非常复杂. 也许我们可以使用ng-include来引用各种模板,但这只限于部分场景. 于是我们可以 ...
- 【Moqui业务逻辑翻译系列】--UBPL index
h2. [UBPL Introduction] ubpl介绍h2. [Actor Definitions] 行为定义h2. General Business Process Stories 通常的商业 ...
- ajax中的application/x-www-form-urlencoded中的使用
ajax中的application/x-www-form-urlencoded中的使用一,HTTP上传的基本知识 在Form元素的语法中,EncType表明提交数据的格式 用 Enctype 属性指定 ...
- 在CentOS7上安装RabbitMQ
安装过程参考官网: Installing on RPM-based Linux (RHEL, CentOS, Fedora, openSUSE) 首先需要安装erlang,参考:http://fedo ...
- hdu1595 dijkstra+枚举
开始的时候想的比较简单,直接枚举所有输入的边,但最后超时:后来就先进行一次dij,记录所有最短路上的边,然后枚举删去这些边: #include<stdio.h> #include<s ...
- Spring 管理数据源
Spring 管理数据源 不管通过何种持久化技术,都必须通过数据连接访问数据库,在Spring中,数据连接是通过数据源获得的.在以往的应用中,数据源一般是Web应用服务器提供的.在Spring中,你不 ...
- config 写入
d代码: # __author__ = liukun # coding:utf-8 line = input("please input the config \n :") #提示 ...