Unity coroutine principle

Coroutine not multi-threaded, the main thread or coroutine inside (Note: the main thread in the Central African Unity Unity is not accessible resources)

1 difference, threads, processes, and coroutines

Process has its own separate heap and stack, heap that is not shared nor shared stack, process scheduling by the operating system

Thread has its own independent stack and heap share, sharing is not shared heap stack, thread scheduling also operating system (this is the standard thread)

Coroutine like threads shared heap and stack are not shared, scheduled coroutine displayed inside the coroutine code by the programmer

An application generally corresponds to a process, a process generally have a main thread, there are a number of worker threads, parallel between threads, coroutines can be opened inside the thread, let the program run at a specific time.

Difference coroutine and threads are: coroutine avoid meaningless schedule, which can improve performance, but because the programmer must take responsibility for their own schedule, at the same time, coroutine also lose the ability to use standard multi-CPU thread .

2, the principle of the coroutine execution Unity

To affix a unity framework diagram of the main line:

In Unity runtime, call the coroutine is to open a IEnumerator (iterator), coroutine started, before executing the yield return and other normal program there is no difference, but when encountered yield return will return immediately, and the function is temporarily suspended. After the next frame is determined encountered FixedUpdate or Update yield return back condition is satisfied, if satisfied performed downward.

Frame according to the main line diagram unity we know, collaborative program mainly after the update () method, lateUpdate before calling method ().

Unity affect the life cycle of coroutines:

通过设置MonoBehaviour脚本的enabled对协程是没有影响的,但如果gameObject.SetActive(false) 则已经启动的协程则完全停止了,即使在Inspector把gameObject 激活还是没有继续执行。也就说协程虽然是在MonoBehvaviour启动的(StartCoroutine)但是协程函数的地位完全是跟MonoBehaviour是一个层次的,不受MonoBehaviour的状态影响,但跟MonoBehaviour脚本一样受gameObject 控制,也应该是和MonoBehaviour脚本一样每帧“轮询” yield 的条件是否满足。
注:WaitForSends()受Time.timeScale影响,当Time.timeScale = 0f时,yieldreturn new WaitForSecond(X)将不会满足。

3、协程的主要应用

协程不是只能做一些简单的延迟,如果只是单纯的暂停几秒然后在执行就完全没有必要开启一个线程。

协程的真正作用是分步做一些比较耗时的事情,比如加载游戏里的资源

using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;

public class Test : MonoBehaviour {

    private int num = 0;

    void Start()
    {
        StartCoroutine(Test1());
    }

    IEnumerator Test1()
    {
        while (num < 30)
        {
            num++;
            Debug.Log(num);
            yield return null;
            Debug.Log("-------------" + num);
        }
    }

    void Update()
    {
        Debug.Log("update" + num);
    }
    void LateUpdate()
    {
        Debug.Log("lateUpdate!" + num);
    }
}

上面的例子很简单就是加载30个对象,如果一次性加载会有卡顿现象,影响游戏体验。这是我们就可以使用协程了,协程是每帧LateUpdate之前执行yield return 之前的代码,LateUpdate之后执行yield return 之后的代码(可以用过上面的小例子看出)。这样我们就可以每帧加载一个直到加载完毕,不会出现卡顿的现象。

Guess you like

Origin blog.csdn.net/qq_38721111/article/details/89394078