Summary of several methods of unity delay execution

Time:2021-10-19
catalogue
  • Update timer
  • Invoke
  • Synergetic process
  • DoTween

When I first contacted unity, I used the timer in update to delay. Later, I found that there are so many more convenient ways to delay execution. Now let’s touch it.

There are four kinds listed in this article. If there are mistakes and omissions, please add and correct them

Update Invoke Synergetic process DoTween
Support parameter input ✖️ ✖️ ✔️ ✖️

The following table shows the implementation status of each scheme under different conditions

Update Invoke Synergetic process DoTween
Time.timeScale = 0 ✔️ ✖️ ✖️ ✔️
activeSelf == false ✖️ ✔️ ✖️ ✔️
enabled == false ✖️ ✔️ ✔️ ✔️
destroy ✖️ ✖️ ✖️ ✔️

Update timer

Time in update and other refresh functions. Timer + = time.deltatime. When the time reaches the expectation, execute the method that needs delay. It should be noted that the delta time. Delta time is a floating-point number that is different for each frame. The timer cannot use = = to judge whether the condition is reached. You need to add a flag bit with > or > =. To ensure that the function can be executed only once. Update will execute normally when time.timescale = 0, but the value of time.deltatime is 0. If you need to use a timer at this time, you can use time.realtimesincestartup

private float delayTime = 1;
    private float timer = 0;
    private bool executed = false;
    void Update(){
        if (executed == false){
            timer += Time.deltaTime;
            if (timer >= 1){
                executed = true;
                DelayFunc();
            }
        }
    }
    private void DelayFunc(){
        Print ("execute test function");
    }

Invoke

The invoke () method is a delegation mechanism for unity3d. Easy to use. But this guy is different from other methods. Hiding objects or setting the script enabled to false will not interrupt the execution of invoke. Invoke supports repeated calls: invokerepeating (“method name”, delay duration, and interval duration of each call)

private float delayTime = 5;
    private void Start(){ 
        Invoke("DelayFunc",delayTime);
        //InvokeRepeating("DelayFunc",delayTime,cdTime);
    }
    private void DelayFunc(){
        Print ("execute test function");
    }

Synergetic process

Coprocessor is essentially an iterator, which is cumbersome to write, but easy to use. You can delay a fixed time or a fixed frame.
Click here is a management class of collaborative process, which supports functions such as pause and resume

private float timer = 0;
    private void Start()
    {
        StartCoroutine("DelayFunc");
        Time.timeScale = 0;
    }
    IEnumerator DelayFunc()
    {
        while ( timer < 30)
        {
            yield return new WaitForSeconds(1);
            timer++;
            Print ("execute test function" + timer); 
        }
    }

DoTween

If you use ugui, you must know dotween. Dotween is a very common simple animation plug-in, but sometimes we can use it to do some strange operations. For example, the delay function can only be executed through SEQ. Pause(); seq.Kill(); Wait for the built-in function to close. When time.timescale = 0, continue to execute the method when hiding objects, disabling scripts, deleting objects, etc.

private void Start()
    {
        seq = DOTween.Sequence();
        seq.AppendInterval(5);
        seq.AppendCallback(DelayFunc);
        seq.SetAutoKill(false);
        seq.SetUpdate(true);// When set to true, it can be executed normally when time. Timescale = 0
        
    }
    void DelayFunc()
    {
        Print ("execute test function"); 
    }

This is the end of this article about the summary of various methods of unity delay execution. For more information about unity delay execution, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!