Unity计时器(全)

news/2024/10/8 2:20:21

Unity计时器

 

 

游戏中有非常多的计时功能,比如:各种cd,以及需要延时调用的方法;

一般实现有一下几种方式:

1.手动计时

float persistTime = 10f
float startTime = Time.time;
if(Time.time - startTime > persistTime)
{Debug.Log("计时结束");
}float curTime = 0;
curTime += Time.deltaTime;
if(curTime > persistTime)
{Debug.Log("计时结束");
}

2:协程

private float persistTime = 10f;
IEnumerator DelayFunc()
{yield return persistTime;Debug.Log("计时结束");
}private void Start()
{StartCoroutine(DelayFunc());
}

3.Invoke回调

private void Start()
{Invoke("DelayFunc", persistTime);
}

计时器功能

计时是为了到特定的时间,执行某个功能或方法;

计时器(Timer):设计了计时暂停,计时重置,计时开始方法,计时中方法,计时结束方法,固定间隔调用方法,计时器可设置复用或单次;

计时管理器(TimerMa):负责倒计时,以及执行计时器方法;

代码:

using System;
using System.Collections.Generic;
using UnityEngine;
using Object = System.Object;public class Timer
{public delegate void IntervalAct(Object args);//总时间和当前持续时间private float curtime = 0;private float totalTime = 0;//激活public bool isActive;//计时结束是否销毁public bool isDestroy;//是否暂停public bool isPause;//间隔事件和间隔事件——Dotprivate float intervalTime = 0;private float curInterval = 0;private IntervalAct onInterval;private Object args;//进入事件public Action onEnter;private bool isOnEnter = false;//持续事件public Action onStay;//退出事件public Action onEnd;public Timer(float totalTime, bool isDestroy = true, bool isPause = false){curtime = 0;this.totalTime = totalTime;isActive = true;this.isDestroy = isDestroy;this.isPause = isPause;TimerMa.I.AddTimer(this);}public void Run(){//暂停计时if (isPause || !isActive)return;if (onEnter != null){if (!isOnEnter){isOnEnter = true;onEnter();}}//持续事件if (onStay != null)onStay();curtime += Time.deltaTime;//间隔事件if (onInterval != null){curInterval += Time.deltaTime;if (curInterval > intervalTime){onInterval(args);curInterval = 0;}}//计时结束if (curtime > totalTime){curtime = 0;isActive = false;if (onEnd != null){onEnd();}}   }//设置间隔事件public void SetInterval(float interval, IntervalAct intervalFunc, Object args = null){this.intervalTime = interval;onInterval = intervalFunc;curInterval = 0;this.args = args;}//重置计时器public void Reset(){curtime = 0;isActive = true;isPause = false;curInterval = 0;isOnEnter = false;}//获取剩余时间public float GetRemainTime(){return totalTime - curtime;}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class TimerMa : MonoBehaviour
{#region 单例private static TimerMa instance;TimerMa() {}public static TimerMa I{get{if (instance == null)instance = new TimerMa();return instance;}}#endregionprivate List<Timer> timerList;private void Awake(){instance = this;timerList = new List<Timer>();}public void AddTimer(Timer t){timerList.Add(t);}void Update(){for (int i = 0; i < timerList.Count;){timerList[i].Run();//计时结束,且需要销毁if(!timerList[i].isActive && timerList[i].isDestroy)timerList.RemoveAt(i);else++i;}}
}

测试计时器

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Object = System.Object;public class Test : MonoBehaviour
{public Text mText1;public Text mText2;private Timer timer;private int count = 0;void Start(){timer = new Timer(5f,false);timer.SetInterval(1f, OnInterval);timer.onEnter = OnStart;timer.onEnd = OnExit;}void Update(){Debug.Log(count);mText1.text = timer.GetRemainTime().ToString("f2");if (Input.GetKeyDown(KeyCode.A)){if (!timer.isPause){timer.isPause = true;mText2.text = "暂停计时";}}if (Input.GetKeyDown(KeyCode.S)){if (timer.isPause){timer.isPause = false;mText2.text = "取消暂停计时";}}if (Input.GetKeyDown(KeyCode.D)){timer.Reset();mText2.text = "重置计时";}}private void OnStart(){mText2.text = "开始计时";}private void OnExit(){mText2.text = "结束计时";}private void OnInterval(Object value){count++;mText2.text = $"间隔事件调用{count}";}
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/28164.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

是面试官放水,还是公司实在是太缺人?这都没挂,华为原来这么容易进...

华为是大企业,是不是很难进去啊?” “在华为做软件测试,能得到很好的发展吗? 一进去就有9.5K,其实也没有想的那么难” 直到现在,心情都还是无比激动! 本人211非科班,之前在字节和腾讯实习过,这次其实没抱着什么特别大的希望投递,没想到华为可以再给我一次机会,还是挺…

路由跳转、相关api、路由守卫、路由两种工作模式

【router基本使用(路由跳转,携带数据)】####### 跳转 #######-js跳转this.$router.push(路径)this.$router.push(对象)-this.$router.push({name:路由别名})-this.$router.push({path:路径})-组件跳转<router-link to="/about"><router-link :to="{n…

H264/AVC-帧内预测相邻像素推导过程

帧内预测过程会以相邻块的像素值做参考,来预测当前块的像素值。以Intra_4x4为例,如下图所示,需要用到的13个相邻像素值,那么如何获取这13个像素值?本文要主要说明如何获取帧内预测所用到的相邻像素。 获取相邻像素的流程如下:找到当前块(可以为4x4、8x8、16x16大小)的左…

R:OTU根据分类级别拆分

输入文件输出文件 rm(list = ls()) setwd("C:\\Users\\Administrator\\Desktop\\microtable") #设置工作目录 library(dplyr) library(tidyr) library(readr)# 读取文件 data <- readLines(1.txt)# 定义分类等级的前缀和列名 prefixes <- c("k__", &…

MySQL排序时, ORDER BY将空值NULL放在最后

我们在日常工作当中;往往业务会提到一些莫名其妙的排序等规则;例如:按照某个字段升序排列,同时空值放在后面;但mysql默认升序排列时空值是在最前面;有下面几个方法: 方法一:ORDER BY 字段 IS NULL ,字段 ;方法二:SELECT * FROM test ORDER BY IF(ISNULL(字段),1,0),字…

《深度学习原理与Pytorch实战》(第二版)(三)11-15章

第11章 神经机器翻译器——端到端机器翻译神经机器翻译,google旗下的NMT 编码-解码模型:用编码器和解码器组成一个翻译机,先用编码器将源信息编码为内部状态,再通过解码器将内部状态解码为目标语言。编码过程对应了阅读源语言句子的过程,解码过程对应了将其重组为目标语言…

STM32F1和STM32F4系列DMA的不同之处——对STM32的DMA的工作机制的一些理解

比较STM32F4和STM32F1系列的DMA控制器,区别主要有三:1)增加了DMA流(Stream)的概念;2)限制了两个DMA控制器的数据流向;3)为每个数据流添加了可配置的FIFO缓冲区。 本文逐一比较了以上三种硬件上的改变带来的功能方面的升级和不同。另外,还大胆猜测了STM32的芯片设计者…

Apache Log4j2远程命令执行漏洞

Log4j2框架下的Lookup查询服务提供了{}字段解析功能,传进去的值会被直接解析。在lookup的{}里面构造Payload,调用JNDI服务获取恶意的class对象,造成了远程代码执行。目录漏洞原理复现反弹shell漏洞修复 AApache Log4j2 是一个基于Java的日志记录工具,被广泛应用于业务系统开…