关于Unity延时的一些方法

在Unity中,延时有几种常用方法。

1、使用Invoke、InvokeRepeating函数,可延迟调用或重复调用特定的函数。

(1)使用Invoke:
 

using UnityEngine;

public class Example : MonoBehaviour
{
    private void Start()
    {
        Invoke("DelayedMethod", 3f);
    }

    private void DelayedMethod()
    {
        Debug.Log("Delayed method executed!");
    }
}

本案例中,DelayedMethod函数将在3s后执行。

(2)使用InvokeRepeating:

using UnityEngine;

public class Example : MonoBehaviour
{
    private void Start()
    {
        InvokeRepeating("RepeatedMethod", 2f, 1f);
    }

    private void RepeatedMethod()
    {
        Debug.Log("Repeated method executed!");
    }
}

本案例中,RepeatedMethod函数将在2s后开始执行,并且之后每个1s重复执行一次。

使用以上两种方法,可以通过以下方法检测或停止Invoke的状态。

(1)IsInvoking() 只要有Invoke在运行,就返回true.

(2)IsInvoking(函数名) 当这个函数正在Invoke的时候返回true

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

如上面的InvokeRepeating,使用CancelInvoke("RepeatedMethod");即可以停止Invoke;

2、使用协程Coroutine实现延时

public class delay : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {        
        StartCoroutine(DelayDo());
    }

    IEnumerator DelayDo()
    {
        yield return new WaitForSeconds(2f);
        Debug.Log("2秒后执行这里");
    }
}

如本案例使用协程延时2s执行后续代码。

3、使用Time.time来记录时间,并通过计算时间差来实现延时。

using UnityEngine;

public class DelayExample : MonoBehaviour
{
    private float startTime;
    private float delayTime = 3f; // 延时时间为3秒

    private void Start()
    {
        // 记录开始时间
        startTime = Time.time;
    }

    private void Update()
    {
        // 计算时间差
        float elapsedTime = Time.time - startTime;

        // 判断是否达到延时时间
        if (elapsedTime >= delayTime)
        {
            // 延时时间已经达到,执行延时后的操作
            DoDelayedAction();
        }
    }

    private void DoDelayedAction()
    {
        // 在这里执行延时后的操作
        Debug.Log("延时操作已完成");
    }
}

4、使用Time.deltaTime来进行时间累加,从而实现延时。

using UnityEngine;

public class DelayExample : MonoBehaviour
{
    private float delayTime = 3f; // 延时时间为3秒
    private float elapsedTime;

    private void Update()
    {
        // 累加时间
        elapsedTime += Time.deltaTime;

        // 判断是否达到延时时间
        if (elapsedTime >= delayTime)
        {
            // 延时时间已经达到,执行延时后的操作
            DoDelayedAction();
        }
    }

    private void DoDelayedAction()
    {
        // 在这里执行延时后的操作
        Debug.Log("延时操作已完成");

        // 重置时间累加
        elapsedTime = 0f;
    }
}

除了以上一些Unity自身可实现的一些方法,我们还可以用其它插件实现延时,如DOTween,Vision Timer。

5、DOTween。

using UnityEngine;
using DG.Tweening;

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

6、Vision Timer。

    vp_Timer.Handle Timer = new vp_Timer.Handle();
    vp_Timer.In(2, new vp_Timer.Callback(() =>{Timer.Cancel();//此处执行延时后函数 }), Timer);

以上就是一些常用的延时方法,不知道还没有其它的。

猜你喜欢

转载自blog.csdn.net/mr_five55/article/details/134221650