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]);

    }

}

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

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值