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

1、使用协程Coroutine

public class Test : MonoBehaviour
{
    // Start is called before the first frame update
    void 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]);

    }

}

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

相关推荐

  1. Unity触发常规方法

    2024-05-10 22:30:04       28 阅读
  2. Unity移动方式

    2024-05-10 22:30:04       60 阅读
  3. Unity笔记:数据持久化方式

    2024-05-10 22:30:04       67 阅读
  4. CSS中常用清除浮动方法

    2024-05-10 22:30:04       24 阅读
  5. DNS故障常见原因及解决方法

    2024-05-10 22:30:04       53 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-10 22:30:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-10 22:30:04       100 阅读
  3. 在Django里面运行非项目文件

    2024-05-10 22:30:04       82 阅读
  4. Python语言-面向对象

    2024-05-10 22:30:04       91 阅读

热门阅读

  1. -Practical Assignment: Isolated Spoken Digit Recognition

    2024-05-10 22:30:04       28 阅读
  2. Managing Digital Design and Web Development MANG6531

    2024-05-10 22:30:04       36 阅读
  3. git報錯:! [rejected] master -> master (fetch first)

    2024-05-10 22:30:04       30 阅读
  4. qt窗口置顶

    2024-05-10 22:30:04       27 阅读
  5. Python 的主要应用领域有哪些?

    2024-05-10 22:30:04       35 阅读
  6. 矩阵的压缩存储

    2024-05-10 22:30:04       30 阅读
  7. js实现定时器

    2024-05-10 22:30:04       27 阅读
  8. UE5缺少SDK,而无法在windows平台打包的解决方法

    2024-05-10 22:30:04       28 阅读