Version: 5.5
public Coroutine StartCoroutine (IEnumerator routine);

설명

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 modelling behaviour 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. There is no guarantee that coroutines end in the same order that they were started, even if they finish in the same frame.

When using JavaScript it is not necessary to use StartCoroutine, the compiler will do this for you. When writing C# code you must call StartCoroutine.

using UnityEngine;
using System.Collections;

// In this example we show how to invoke a coroutine and continue executing // the function in parallel.

public class ExampleClass : MonoBehaviour { // In this example we show how to invoke a coroutine and // continue executing the function in parallel.

private IEnumerator coroutine;

void Start() { // - After 0 seconds, prints "Starting 0.0" // - After 0 seconds, prints "Before WaitAndPrint Finishes 0.0" // - After 2 seconds, prints "WaitAndPrint 2.0" print("Starting " + Time.time);

// Start function WaitAndPrint as a coroutine.

coroutine = WaitAndPrint(2.0f); StartCoroutine(coroutine);

print("Before WaitAndPrint Finishes " + Time.time); }

// every 2 seconds perform the print() private IEnumerator WaitAndPrint(float waitTime) { while (true) { yield return new WaitForSeconds(waitTime); print("WaitAndPrint " + Time.time); } } }

Another example:

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { IEnumerator Start() { print("Starting " + Time.time); yield return StartCoroutine(WaitAndPrint(2.0F)); print("Done " + Time.time); } IEnumerator WaitAndPrint(float waitTime) { yield return new WaitForSeconds(waitTime); print("WaitAndPrint " + Time.time); } }

public Coroutine StartCoroutine (string methodName, object value= null);

설명

Starts a coroutine named methodName.

In most cases you want to use the StartCoroutine variation above. However StartCoroutine using a string method name allows you to use StopCoroutine with a specific method name. The downside is that the string version has a higher runtime overhead to start the coroutine and you can pass only one parameter.

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { IEnumerator Start() { StartCoroutine("DoSomething", 2.0F); yield return new WaitForSeconds(1); StopCoroutine("DoSomething"); } IEnumerator DoSomething(float someParameter) { while (true) { print("DoSomething Loop"); yield return null; } } }