StartCoroutine

Starts a coroutine.

The execution of a coroutine can be paused at any point using the yield statement. The yield return value specifies when the coroutine is resumed. Coroutines are excellent when modeling behavior over several frames. Coroutines have virtually no performance overhead. StartCoroutine function always returns immediately, however you can yield the result. This will wait until the coroutine has finished execution.

Tip

When using JavaScript it is not necessary to use StartCoroutine, as the compiler will do this for you. When writing C# code you must call StartCoroutine. (For more details, refer to Appendix C, Major differences Between C# and Unity JavaScript.)

In the following example, we show how to invoke a coroutine and continue executing the function in parallel:

function Start() {
  // Starting = 0.0
  Debug.Log ("Starting = " + Time.time);

  // StartCoroutine WaitAndPrint (In JavaScript, you can also use WaitAndPrint(5.0) which will get the same result.
  StartCoroutine(WaitAndPrint(5.0)); 

  // Before WaitAndPrint = 5.0
  Debug.Log ("Before WaitAndPrint = " + Time.time);
}

function WaitAndPrint(waitTime : float) {
  //Suspend execution for 5 seconds 
  yield WaitForSeconds(waitTime);

  // WaitAndPrint = 5.0
  Debug.Log ("WaitAndPrint = " + Time.time);
}

The following example will wait until the WaitAndPrint function is finished and then continues executing the rest of the code in the Start function:

function Start() {
  // Starting = 0.0
  Debug.Log ("Starting = " + Time.time);

  // StartCoroutine WaitAndPrint (In JavaScript, you can also use yield WaitAndPrint(5.0) which will get the same result.
  yield StartCoroutine(WaitAndPrint(5.0)); 

  // Done WaitAndPrint = 5.0
  Debug.Log ("Done WaitAndPrint = " + Time.time);
}

function WaitAndPrint(waitTime : float) {
  //Suspend execution for 5 seconds 
  yield WaitForSeconds(waitTime);

  // WaitAndPrint = 5.0
  Debug.Log ("WaitAndPrint = " + Time.time);
}

Using StartCoroutine with method name (string)

In most cases, you would want to use the preceding StartCoroutine variation. However, StartCoroutine using a string method name allows you to use StopCoroutine with a specific method name.

Note

The downside is that the string version has a higher runtime overhead to start the coroutine and you can pass only one parameter.

In the following example, we show how to invoke a coroutine using a string name and how to stop it:

function Start() {
  // Start Coroutine DoSomething
  StartCoroutine("DoSomething", 5.0); 

  //  Wait for 2 seconds
  yield WaitForSeconds(2.0);

  // Stop Coroutine DoSomething
  StopCoroutine("DoSomething");
}

function DoSomething (someParameter : float) {
  while (true) {
    // DoSomething Loop
    Debug.Log ("DoSomething Loop = " + Time.time);
    // Yield execution of this coroutine and return to the main loop until next frame
    yield;
  }
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.165.115