当前位置: 首页 > news >正文

Unity延时触发的几种常规方法

目录

  • 1、使用协程Coroutine
  • 2、使用Invoke、InvokeRepeating函数
  • 3、使用Time.time
  • 4、使用Time.deltaTime
  • 5、使用DOTween。
  • 6、使用Vision Timer。

1、使用协程Coroutine

public class Test : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){        StartCoroutine(DelayExecute());}IEnumerator DelayExecute(){yield return new WaitForSeconds(2f);Debug.Log("延迟2秒后执行");}
}

2、使用Invoke、InvokeRepeating函数

(1)使用Invoke:

using UnityEngine;public class Test : MonoBehaviour
{private void Start(){Invoke("DelayedExeCute", 2f); //2秒后执行}private void DelayedExeCute(){Debug.Log("延迟后,执行!");}
}

(2)使用InvokeRepeating:

using UnityEngine;public class Test: MonoBehaviour
{private void Start(){InvokeRepeating("DelayedExeCute", 2f, 2f);//2s后开始执行,并且之后每个2s重复执行一次。}private void DelayedExeCute(){Debug.Log("延迟后,执行!");}
}

注意:
可通过以下一些集成好的方法检测或停止Invoke的状态。

  1. IsInvoking(): 判断是否有通过Invoke方式调用的函数,只要有Invoke在运行,就返回true.

  2. IsInvoking(函数名): 指定函数名称,当这个函数正在Invoke的时候返回true

  3. CancelInvoke(函数名): 取消所有Invoke或者对应Invoke

3、使用Time.time

using UnityEngine;public class Test : MonoBehaviour
{private float startTime;private float delayTime = 2f; // 延时时间为2秒private flaot elapsedTime=0;private void Start(){startTime = Time.time;// 记录开始时间}private void Update(){elapsedTime = Time.time - startTime;// 时间差if (elapsedTime >= delayTime)// 判断是否达到延时时间{ExecuteAction();//已经达到,执行延时后的操作}}private void ExecuteAction(){Debug.Log("开始执行操作");}
}

4、使用Time.deltaTime

using UnityEngine;public class Test: MonoBehaviour
{private float delayTime = 3f; // 延时时间为3秒private float elapsedTime=0;private void Update(){elapsedTime += Time.deltaTime;  // 累加时间if (elapsedTime >= delayTime)// 判断是否达到延时时间{ExecuteAction();//已经达到,执行延时后的操作}}private void ExecuteAction(){Debug.Log("开始执行操作");elapsedTime = 0f; //清空重置}
}

5、使用DOTween。

using UnityEngine;
using DG.Tweening;public class Test: MonoBehaviour
{private void Start(){float delayTime = 2f;   // 延时2秒后执行回调函数DOTween.To(() => 0, x => { }, 0, delayTime).OnComplete(() =>{Debug.Log("延时结束!");});}}

6、使用Vision Timer。

public class Test: MonoBehaviour
{private void Start(){vp_Timer.In(2f, ExecuteMethod);//2秒后调用vp_Timer.In(2f, ExecuteMethod, 3);//2秒后调用,间隔1秒调用3次vp_Timer.In(2f, ExecuteMethod,4, 1); //2秒后调用,间隔1秒调用4次vp_Timer.In(2f, ExecuteMethod, 0, 1);//2秒后调用,间隔1秒调用无限次vp_Timer.In(1.0f, MethodWithArgument, "CanShuValue");//带参数调用vp_Timer.In(1.0f, MethodWithArguments,new object[] { "A", 1, 2 }); //带多个参数调用//vp_Timer.CancelAll(); //取消所有定时器//vp_Timer.CancelAll("SomeMethod"); //取消定时器}void ExecuteMethod(){Debug.Log("延迟后,开始执行");}void MethodWithArgument(object o){string s = (string)o;Debug.Log("传过来的参数:"+s);}void MethodWithArguments(object o){object[] arg = (object[])o;Debug.Log("1: " + (string)arg[0]+ ", 2:" + (int)arg[1]+ ", 3:" + (int)arg[2]);}}

这里是井队,天高任鸟飞,海阔凭鱼跃,点个关注不迷路,我们下期再见。

http://www.lryc.cn/news/345801.html

相关文章:

  • CSS文字描边,文字间隔,div自定义形状切割
  • XWiki 服务没有正确部署在tomcat中,如何尝试手动重新部署?
  • 【退役之重学Java】关于 Redis
  • DateKit
  • 百度智能云数据仓库 Palo 实战课程
  • 服务端JavaScript(Node.js)与去IO编程:Node.js的事件驱动和非阻塞IO模型,它是如何使JavaScript走向后端的
  • 一键局域网共享工具
  • python实现把doc文件批量转化为docx
  • WEB基础---反射
  • impdp恢复表后发现比原表多了100多行
  • Jupyter配置远程访问的密码
  • Windows下通过MySQL Installer安装MySQL服务
  • C语言 [力扣]详解环形链表和环形链表II
  • Threejs 学习笔记 | 灯光与阴影
  • SSH:安全远程访问的基石
  • 杰发科技AC7801——ADC之Bandgap和内部温度计算
  • 了解 macOS 中的系统完整性保护 (SIP):开启与关闭
  • 【Linux】简易进度条的实现
  • Docker + Django跨域解决方案
  • Maven 插件使用
  • 【HMGD】GD32/STM32 DMA接收不定长串口数据
  • 局域网手机端远程控制手机
  • 如何在OpenWrt软路由中增加一个新功能
  • 【linux】vmtouch文件缓存管理工具
  • 论文阅读:The Unreasonable Ineffectiveness of the Deeper Layers 层剪枝与模型嫁接的“双生花”
  • Python批量备份华为设备配置到FTP服务器
  • Java虚拟机(JVM)中确保资源及时释放的策略
  • 04-Fortran基础--Fortran数组和矩阵运算
  • el-select选项框内容过长
  • K8S面试题学习5