Unity3D 计时器的实现代码(三种写法总结)
作者:加油IT 时间:2022-02-10 08:51:08
1、每帧检查
定义一个时间变量 timer,每帧将此时间减去帧间隔时间 Time.deltaTime,如果小于或者等于零,说明定时器到了,执行相应功能代码,将此定时器重置,代码如下:
public float timer = 1.0f;
// Update is called once per frame
void Update() {
timer -= Time.deltaTime;
if (timer <= 0) {
Debug.Log(string.Format("Timer1 is up !!! time=${0}", Time.time));
timer = 1.0f;
}
}
2、利用协程
在协程中返回需要等待的时间,直接看代码便明白:
// Use this for initialization
void Start() {
StartCoroutine(Timer());
}
IEnumerator Timer() {
while (true) {
yield return new WaitForSeconds(1.0f);
Debug.Log(string.Format("Timer2 is up !!! time=${0}", Time.time));
}
}
3、延迟调用
使用 MonoBehaviour.Invoke,两个参数,分别是要调用的方法名和延时调用的时间。代码如下:
// Use this for initialization
void Start() {
Invoke("Timer", 1.0f);
}
void Timer() {
Debug.Log(string.Format("Timer3 is up !!! time=${0}", Time.time));
Invoke("Timer", 1.0f);
}
补充:unity3D 实现一个时分秒的计时器:格式为00:00:00
简单粗暴,上代码。
public class Clock : MonoBehaviour {
public Text m_ClockText;
private float m_Timer;
private int m_Hour;//时
private int m_Minute;//分
private int m_Second;//秒
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
m_Timer += Time.deltaTime;
m_Second = (int)m_Timer;
if (m_Second > 59.0f)
{
m_Second = (int)(m_Timer - (m_Minute * 60));
}
m_Minute = (int)(m_Timer / 60);
if (m_Minute > 59.0f)
{
m_Minute = (int)(m_Minute - (m_Hour * 60));
}
m_Hour = m_Minute / 60;
if (m_Hour >= 24.0f)
{
m_Timer = 0;
}
m_ClockText.text = string.Format("{0:d2}:{1:d2}:{2:d2}", m_Hour,m_Minute,m_Second);
}
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。
来源:https://blog.csdn.net/xiumang/article/details/88750110
标签:Unity3D,计时器
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
一文带你了解RabbitMQ消息转换器
2023-11-15 23:22:28
![](https://img.aspxhome.com/file/2023/4/64384_0s.jpg)
Java枚举详解及使用实例(涵盖了所有典型用法)
2023-06-29 23:17:55
java接口返回参数按照请求参数进行排序方式
2023-02-19 07:30:05
![](https://img.aspxhome.com/file/2023/1/64501_0s.png)
浅谈关于Mybatis的mapper-locations配置问题
2023-09-24 06:06:16
![](https://img.aspxhome.com/file/2023/5/67465_0s.jpg)
java ReentrantLock条件锁实现原理示例详解
2023-12-12 02:36:13
![](https://img.aspxhome.com/file/2023/8/66508_0s.png)
图文详解Maven工程打jar包的N种方式
2022-12-12 21:36:23
![](https://img.aspxhome.com/file/2023/9/60359_0s.png)
Spring+EHcache缓存实例详解
2023-12-12 20:27:51
![](https://img.aspxhome.com/file/2023/2/77932_0s.jpg)
java实现单链表、双向链表
2023-02-09 03:15:59
Java之InputStreamReader类的实现
2023-09-03 01:28:17
C# string格式的日期时间字符串转为DateTime类型的方法
2022-04-07 09:34:30
Spring Boot JPA Repository之existsBy查询方法失效的解决
2023-01-27 18:36:10
利用Java写一个学生管理系统
2023-09-24 17:06:54
![](https://img.aspxhome.com/file/2023/1/61071_0s.jpg)
SpringBoot 上传文件判空以及格式检验流程
2023-01-19 05:07:36
![](https://img.aspxhome.com/file/2023/5/61405_0s.png)
详解Java如何实现小顶堆和大顶堆
2023-11-10 04:03:05
![](https://img.aspxhome.com/file/2023/9/59179_0s.png)
seata-1.4.0安装及在springcloud中使用详解
2023-02-21 00:49:21
![](https://img.aspxhome.com/file/2023/5/62835_0s.png)
java实现邮件发送
2022-06-03 02:48:20
![](https://img.aspxhome.com/file/2023/2/68762_0s.jpg)
解决IntelliJ IDEA中鼠标拖动选择为矩形区域问题
2022-04-03 21:08:51
![](https://img.aspxhome.com/file/2023/6/65176_0s.png)
利用logback 设置不同包下的日志级别
2022-08-11 20:05:43
Android中bindService基本使用方法概述
2023-08-05 19:18:56
![](https://img.aspxhome.com/file/2023/6/85446_0s.png)
Java面向对象程序设计多态性示例
2021-09-24 11:57:57