原地址:http://blog.sina.com.cn/s/blog_5b6cb9500101aejs.html

https://github.com/xuzhiping7/Unity3d-Timer

项目中管理计时器太混乱难看了,用好听点的话来说就是代码不优雅。

 
想了下就随手简单写了个时间管理模块了。其实有好几种实现方式的,但是选用了U3D最为合适和简单的方式写。效率可能不高,但做小游戏是压根没问题的了。
 
原理简单点来说就是统一管理计时器。
 
每个计时器有自己的开始、暂停、结束、重新开始。当计时结束之后则调用相应的一个或者多个函数。
 
Timer.cs算是基类,TimerManager.cs则是管理每一个计时器的管理程序。根据不同的项目改至适用即可。
 
就那样,代码非常简单。就是一个委托回调。
 
具体代码放到GITHUB了,有兴趣的同学可以上去看看。
 
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的更多相关文章

  1. (转)[Unity3D]计时器/Timer

    http://blog.sina.com.cn/s/blog_5b6cb9500101aejs.html 项目中管理计时器太混乱难看了,用好听点的话来说就是代码不优雅.   想了下就随手简单写了个时间 ...

  2. 计时器 Timer

    计时器 Timer 不多说了,守则.

  3. C# - 计时器Timer

    System.Timers.Timer 服务器计时器,允许指定在应用程序中引发事件的重复时间间隔. using System.Timers: // 在应用程序中生成定期事件 public class ...

  4. Android中三种计时器Timer、CountDownTimer、handler.postDelayed的使用

    在android开发中,我们常常需要用到计时器,倒计时多少秒后再执行相应的功能,下面我就分别来讲讲这三种常用的计时的方法. 一.CountDownTimer 该类是个抽象类,如果要使用这个类中的方法, ...

  5. 松软科技课堂:索引器计时器Timer

    在.NET中有三种计时器:1.System.Windows.Forms命名空间下的Timer控件,它直接继承自Componet.Timer控件只有绑定了Tick事件和设置Enabled=True后才会 ...

  6. Java计时器Timer和TimerTask用法

    package com.sy.game.test; import java.util.Timer; import java.util.TimerTask; public class TimeTask ...

  7. C# 如何用计时器Timer控件实现停留几秒再做切换窗体的操作

    C# Timer用法及实例详解 关于C# Timer类  在C#里关于定时器类就有3个 C# Timer使用的方法1.定义在System.Windows.Forms里 C# Timer使用的方法2.定 ...

  8. 简单实现一个Unity3d的Timer

    数量使用的不太多,没有实现对象池. using System.Collections; using System.Collections.Generic; using UnityEngine; usi ...

  9. 计时器timer的使用

    https://www.cnblogs.com/ILoveSleep/archive/2013/06/12/3133322.html

随机推荐

  1. 第十章:Javascript子集和扩展

    本章讨论javascript的集和超集,其中子集的定义大部分处于安全考虑.只有使用这门语言的一个安全的子集编写脚本,才能让代码执行的更安全.更稳定.ECMScript3标准是1999年版本的,10年后 ...

  2. MySQL性能分析

    第一步 检查系统的状态 通过操作系统的一些工具检查系统的状态,比如CPU.内存.交换.磁盘的利用率,根据经验或与系统正常时的状态相比对,有时系统表面上看起来看空闲,这也可能不是一个正常的状态,因为cp ...

  3. 【codevs 1296】营业额统计 水~~

    今天下午先写一个Splay水题来复习一下Splay模板.是不是有点太水了做这种水题我有点良心不安. 可笑的是一开始我竟然WA了一组,看来是我低估水题的数据范围了,我是空节点直接返回inf或-inf,明 ...

  4. 什么是POJO?

    本文转载自百度文库,详细出处请参考: http://wenku.baidu.com/view/4a9ad533ee06eff9aef80765.html 我认为写的很准确,很抱歉没有找到作者的名字! ...

  5. log4j2 使用说明

    因近期需要编写J2EE程序,所以简单学习了Log4j2,这里把我学习的一些信息做记录: 1.从HelloWorld开始 参考:http://logging.apache.org/log4j/2.x/m ...

  6. DLX模型问题

    问题:sevenzero liked Warcraft very much, but he haven't practiced it for several years after being add ...

  7. C# WPF 显示图片和视频显示 EmuguCv、AForge.Net测试(续)

    介绍 本文是接着上文<C# WPF 显示图片和视频显示 EmuguCv.AForge.Net测试>写的,建议先看下上文,因为有些代码还需要了解. 增添 接着上文的代码,我们可以在事件处理方 ...

  8. POJ1860Currency Exchange(Bellman + 正权回路)

    Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 23938   Accepted: 867 ...

  9. javascript学习随笔(二)原型prototype

    JavaScript三类方法: 1.类方法:2.对象方法:3.原型方法;注意三者异同 例: function People(name){ this.name=name; //对象方法 this.Int ...

  10. C标准函数库(常用部分)