This article is translated from Unity Official document: https://docs.unity3d.com/Manual/Coroutines.html proper noun: coroutine process alpha opacity when you call a function, It will run until the end before returning. This actually means that any behavior that occurs in a function must occur in one frame update, and a function call cannot be used to contain a process animation or sequence of events. For example, consider a task that progressively reduces the alpha (opacity) value of an object until it becomes completely invisible.
void Fade () { for (float00.1f) { = Renderer.material.color; = F; = c; }}
As you can see, the Fade () function does not have the effect you expect. To make the gradient visible, you must reduce the alpha value by a series of frame updates to show the middle value being rendered. However, the function will all be executed in a single update, and the middle value will never be seen and the object will disappear immediately. You can handle a similar situation by adding Code to the update () function, which is based on a step-by-frame gradient. However, it is often more convenient to use coroutine to accomplish such tasks. Coroutine is like this function, which has the ability to pause execution and return control to unity, but continues to execute in the next frame. In C #, Coroutine is declared this way:
IEnumerator Fade () { for (float00.1f) { = Renderer.material.color; = F; = C; yield return NULL ; }}
It is essentially a function that uses a return type of IEnumerator declaration and a yield return statement that is contained somewhere in the body. The yield return line is the point at which the pause is performed and will be returned to the next frame. You need to set up the run of the starcoroutine through the.
void Update () { if (Input.getkeydown ("F")) { Startcoroutine ( "Fade");} }
In Unityscript, things are a little bit simpler. Any function that contains a yield statement is understood as a coroutine, and the Ieumerator return type does not need to be explicitly declared.
function Fade () { for (var f = 1.0; F >= 0; F-= 0.1) {var c = Render Er.material.color; = F; = C; yield; }}
Similarly, you can start the coroutine in Unityscript, just as a normal function call.
function Update () { if (Input.getkeydown ("F")) { Fade ();} }
You will notice that in the fade () function, the loop counter maintains its correct value during the declaration period of the coroutine. In fact, any variable or parameter will be properly preserved between yield.by default, after yield, a coroutine is restored in this frame, but you can also use Waitforseconds to introduce time delays. This is true in the Unityscript:
function Fade () { for (var f = 1.0; F >= 0; F-= 0.1) {var c = Render Er.material.color; = F; = C; Yield Waitforseconds (0.1);} }
This can be used as a way to achieve a similar propagation effect over time, but it is also a useful optimization. Many of the tasks in the game need to be done on a regular basis, and the most obvious way is to include them in the update function. However, this function is usually called multiple times per second. When a task does not need to be repeated frequently, you can put it into a coroutine to update periodically, but not every frame. An example might be to warn the player whether the enemy is nearby. The general Unityscript code looks like this:
function Proximitycheck () { for (int i = 0; i < enemies. Length; i++) { if (Vector3.distance (transform.position, enemies[i].transform.position) < dangerdistance) { returntrue; } } return false ;}
If there are many enemies and then call this function, each frame will introduce a significant overhead. However, you can use the coroutine to invoke it every few seconds:
IEnumerator Docheck () { for(;;) { proximitycheck; return New waitforseconds (. 1f); }}
This will significantly reduce the number of checks, without any noticeable impact on gameplay. Note: When a monobehaviour is disabled (disable), the co-process (Coroutine) does not stop, but only when it is completely destroyed (destroyed). You can use Monobehaviour.stopcoroutine and monobehaviour.stopallcoroutines to stop the execution of the co-process (Coroutine). When the Monobehaviour is destroyed (destroyed), the co-process will also stop.
Unity script Programming--co-process (Coroutine)