[GitHub] - Unity Timer
https://github.com/akbiggs/UnityTimer#unity-timer
Run actions after a delay in Unity3D.
This library has been battle-tested and hardened throughout numerous projects, including the award-winning Pitfall Planet.
Written by Alexander Biggs + Adam Robinson-Yu.
Installation
To get the latest release of UnityTimer, head over to the Releases page and download the Timer.unitypackage file from the latest release. Then if you have a Unity project open, you can open the .unitypackage file to install the scripts into your project.
Alternatively, if you like to live on the bleeding edge, you can clone this repository into your project's Assets folder. However, we do not guarantee this will give you a stable version.
Basic Example
The Unity Timer package provides the following method for creating timers:
/// <summary>
/// Register a new timer that should fire an event after a certain amount of time
/// has elapsed.
/// </summary>
/// <param name="duration">The time to wait before the timer should fire, in seconds.</param>
/// <param name="onComplete">An action to fire when the timer completes.</param>
public static Timer Register(float duration, Action onComplete);
The method is called like this:
// Log "Hello World" after five seconds. Timer.Register(5f, () => Debug.Log("Hello World"));
Motivation
Out of the box, without this library, there are two main ways of handling timers in Unity:
- Use a coroutine with the WaitForSeconds method.
- Store the time that your timer started in a private variable (e.g.
startTime = Time.time
), then check in an Update call ifTime.time - startTime >= timerDuration
.
The first method is verbose, forcing you to refactor your code to use IEnumerator functions. Furthermore, it necessitates having access to a MonoBehaviour instance to start the coroutine, meaning that solution will not work in non-MonoBehaviour classes. Finally, there is no way to prevent WaitForSeconds from being affected by changes to the time scale.
The second method is error-prone, and hides away the actual game logic that you are trying to express.
This library alleviates both of these concerns, making it easy to add an easy-to-read, expressive timer to any class in your Unity project.
Features
Make a timer repeat by setting isLooped
to true.
// Call the player's jump method every two seconds. Timer.Register(2f, player.Jump, isLooped: true);
Cancel a timer after calling it.
Timer timer; void Start() {
timer = Timer.Register(2f, () => Debug.Log("You won't see this text if you press X."));
} void Update() {
if (Input.GetKeyDown(KeyCode.X)) {
Timer.Cancel(timer);
}
}
Measure time by realtimeSinceStartup instead of scaled game time by setting useRealTime
to true.
// Let's say you pause your game by setting the timescale to 0.
Time.timeScale = 0f; // ...Then set useRealTime so this timer will still fire even though the game time isn't progressing.
Timer.Register(1f, this.HandlePausedGameState, useRealTime: true);
Attach the timer to a MonoBehaviour so that the timer is destroyed when the MonoBehaviour is.
Very often, a timer called from a MonoBehaviour will manipulate that behaviour's state. Thus, it is common practice to cancel the timer in the OnDestroy method of the MonoBehaviour. We've added a convenient extension method that attaches a Timer to a MonoBehaviour such that it will automatically cancel the timer when the MonoBehaviour is detected as null.
public class CoolMonoBehaviour : MonoBehaviour { void Start() {
// Use the AttachTimer extension method to create a timer that is destroyed when this
// object is destroyed.
this.AttachTimer(5f, () => { // If this code runs after the object is destroyed, a null reference will be thrown,
// which could corrupt game state.
this.gameObject.transform.position = Vector3.zero;
});
} void Update() {
// This code could destroy the object at any time!
if (Input.GetKeyDown(KeyCode.X)) {
GameObject.Destroy(this.gameObject);
}
}
}
Update a value gradually over time using the onUpdate
callback.
// Change a color from white to red over the course of five seconds.
Color color = Color.white;
float transitionDuration = 5f; Timer.Register(transitionDuration,
onUpdate: secondsElapsed => color.r = 255 * (secondsElapsed / transitionDuration),
onComplete: () => Debug.Log("Color is now red"));
A number of other useful features are included!
- timer.Pause()
- timer.Resume()
- timer.GetTimeRemaining()
- timer.GetRatioComplete()
- timer.isDone
A test scene + script demoing all the features is included with the package in the Timer/Example
folder.
Usage Notes / Caveats
- This library does not currently seem to work on the Hololens. We are looking into a solution for this.
- All timers are destroyed when changing scenes. This behaviour is typically desired, and it happens because timers are updated by a TimerController that is also destroyed when the scene changes. Note that as a result of this, creating a Timer when the scene is being closed, e.g. in an object's OnDestroy method, will result in a Unity error when the TimerController is spawned..
[GitHub] - Unity Timer的更多相关文章
- 搜刮一些开源项目的APP
iOS完整App资源收集 <iOS完整app资源收集> <GitHub 上有哪些完整的 iOS-App 源码值得参考?> <GitHub 上有哪些完整的 iOS-App ...
- Unity时钟定时器插件——Vision Timer源码分析之一
因为项目中,UI的所有模块都没有MonBehaviour类(纯粹的C#类),只有像NGUI的基本组件的类是继承MonoBehaviour.因为没有继承MonoBehaviour,这也不能使用Updat ...
- UNITY中有Timer
using UnityEngine; using System.Collections; using System.Timers; public class NewBehaviourScript : ...
- Unity时钟定时器插件——Vision Timer源码分析之二
Unity时钟定时器插件——Vision Timer源码分析之二 By D.S.Qiu 尊重他人的劳动,支持原创,转载请注明出处:http.dsqiu.iteye.com 前面的已经介绍了vp_T ...
- Unity Github 项目收集
http://gad.qq.com/article/detail/24048 重磅推荐: Github 热门 Unity Assets 查询:http://unitylist.com/browse 最 ...
- 最棒的Unity Github 项目收集(2016)
http://1darray.com/blog/2016/03/08/best-unity-github-repositories/ List of best public GitHub reposi ...
- 【Unity】制作简易定时器(Timer)
最近开始学习Unity,也想开始学习写一些简单的博客. 在网上学习了一些关于定时器的写法,在此简单总结一下,方便自己以后用到时查阅. 需求:制作定时器,运行3秒后执行第一次,之后每隔3秒执行一次操作. ...
- dynamic bone unity github
https://github.com/unity3d-jp/unitychan-crs 我发现我总找不到以前的东西.. https://www.cnblogs.com/alps/p/8284577.h ...
- github for unity
随机推荐
- 序列化表单为json对象,datagrid带额外参提交一次查询 后台用Spring data JPA 实现带条件的分页查询 多表关联查询
查询窗口中可以设置很多查询条件 表单中输入的内容转为datagrid的load方法所需的查询条件向原请求地址再次提出新的查询,将结果显示在datagrid中 转换方法看代码注释 <td cols ...
- xcode 快捷键大全、XCode常用快捷键图文介绍
其实就是设置里面的快捷键变成了文字版,刚开始用Xcode是不是发现以前熟悉的开发环境的快捷键都不能用了?怎么快捷运行,停止,编辑等等.都不一样了.快速的掌握这些快捷键,能提供开发的效率. 其实快捷键在 ...
- 开发的服务集群部署方案,以etcd为基础(java)
当前有很多服务集群部署,但是对于我们自己开发的服务系统怎么样能够解决部署问题,对大家很麻烦和笨重. 首先,我想说对于我们国内,小公司小系统比较多.大型系统毕竟少数,向阿里云看齐的不多.其实所谓的需要集 ...
- 【shell脚本学习-3】
part-1 #!/bin/bash:<<FTP#test [ 1 -eq 2] #条件测试x="abc" #不允许有空格y="abc" [ &qu ...
- 在Liunx上搭建FTP并配置用户权限
伴随着.Net Core的开源,公司前几天上了新的Liunx服务器,我在前几篇文章中介绍了如何搭建环境以及部署.Net Core应用. 然后,今天客户和我说想自己给网站做推广,需要用FTP链接我们的服 ...
- ECSHOP快递单号查询插件圆通V8.2专版
本ECSHOP快递物流单号跟踪插件提供国内外近2000家快递物流订单单号查询服务例如申通快递.顺丰快递.圆通快递.EMS快递.汇通快递.宅急送快递.德邦物流.百世快递.汇通快递.中通快递.天天快递等知 ...
- zabbix监控MySQL服务状态
Mysql模板使用 在zabbix_agent配置文件中加入监控配置 vim etc/zabbix_agentd.conf ... UserParameter=mysql.version,mysqla ...
- docker和docker compose常用操作命令
首先区分一下docker中几个概念 Image:镜像,相当于一个root文件系统,不包含任何动态数据 Container:容器,镜像运行时的实体,实质是进程,容器进程运行于属于自己的独立的命名空间 d ...
- ruby 比较符号==, ===, eql?, equal?
“==” 最常见的相等性判断 “==” 使用最频繁,它通常用于对象的值相等性(语义相等)判断,在 Object 的方法定义中,“==” 比较两个对象的 object_id 是否一致,通常子类都会重写覆 ...
- PAT-B java实现
注意:java提交PAT时,不需要加package : 类名必须是Main. 1001 害死人不偿命的(3n+1)猜想 (15) 输入格式:每个测试输入包含1个测试用例,即给出自然数n的值. 输出格式 ...