言語: 日本語
  • C#
  • JS
  • Boo

スクリプト言語

お好みのスクリプト言語を選択すると、サンプルコードがその言語で表示されます。

MonoBehaviour.StartCoroutine

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Sumbission failed

For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Switch to Manual
public function StartCoroutine(routine: IEnumerator): Coroutine;
public Coroutine StartCoroutine(IEnumerator routine);
public def StartCoroutine(routine as IEnumerator) as Coroutine

Description

コルーチンを開始します

コルーチンの実行はyield文を使用することによって、任意のタイミングで一時停止することができます。 コルーチンが再開される時のyieldの戻り値を指定します 複数のフレームにわたって動作をモデリングするときにコルーチンは優れています。 コルーチンは実質的にパフォーマンスのオーバーヘットはありません。 StartCoroutine関数は常にすぐに返りますが、yield文を使うことができます。 コルーチンの実行が完了するまで待機します。 JavaScriptを使用しているときはStartCoroutineを使う必要はありません。コンパイラが自動で使用します。 C#のコードではStartCoroutineを使用する必要があります。

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

	function 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. And continue execution while it is running
	
		// this is the same as WaitAndPrint(2.0) as the compiler does it for you automatically
		StartCoroutine(WaitAndPrint(2.0)); 
		print ("Before WaitAndPrint Finishes " + Time.time);
	}

	function WaitAndPrint (waitTime : float) {
		// suspend execution for waitTime seconds
		yield WaitForSeconds (waitTime);
		print ("WaitAndPrint "+ Time.time);
	}
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    void Start() {
        print("Starting " + Time.time);
        StartCoroutine(WaitAndPrint(2.0F));
        print("Before WaitAndPrint Finishes " + Time.time);
    }
    IEnumerator WaitAndPrint(float waitTime) {
        yield return new WaitForSeconds(waitTime);
        print("WaitAndPrint " + Time.time);
    }
}
import UnityEngine
import System.Collections

public class ExampleClass(MonoBehaviour):

	def Start() as void:
		print(('Starting ' + Time.time))
		StartCoroutine(WaitAndPrint(2.0F))
		print(('Before WaitAndPrint Finishes ' + Time.time))

	def WaitAndPrint(waitTime as float) as IEnumerator:
		yield WaitForSeconds(waitTime)
		print(('WaitAndPrint ' + Time.time))

他の例:

	// In this example we show how to invoke a coroutine and wait until it 
	// is completed
	
	function Start() {
		// - After 0 seconds, prints "Starting 0.0"
		// - After 2 seconds, prints "WaitAndPrint 2.0"
		// - After 2 seconds, prints "Done 2.0"
		print ("Starting " + Time.time);
		// Start function WaitAndPrint as a coroutine. And wait until it is completed.
		// the same as yield WaitAndPrint(2.0);
		yield StartCoroutine(WaitAndPrint(2.0));
		print ("Done " + Time.time);
	}

	function WaitAndPrint (waitTime : float) {
		// suspend execution for waitTime seconds
		yield WaitForSeconds (waitTime);
		print ("WaitAndPrint "+ Time.time);
	}
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);
    }
}
import UnityEngine
import System.Collections

public class ExampleClass(MonoBehaviour):

	def Start() as IEnumerator:
		print(('Starting ' + Time.time))
		yield StartCoroutine(WaitAndPrint(2.0F))
		print(('Done ' + Time.time))

	def WaitAndPrint(waitTime as float) as IEnumerator:
		yield WaitForSeconds(waitTime)
		print(('WaitAndPrint ' + Time.time))

public function StartCoroutine(methodName: string, value: object = null): Coroutine;
public Coroutine StartCoroutine(string methodName, object value = null);
public def StartCoroutine(methodName as string, value as object = null) as Coroutine

Description

コルーチンを開始するメソッド名

殆どの場合では、上記のような様々なバリエーションのStartCoroutineを使用します。 しかし、メソッド名を使用したStartCoroutineを使用すると、特定のメソッド名の StopCoroutine を使用することができます。 欠点は、文字列バージョンはコルーチンの開始するために高いランタイムのオーバーヘッドを持つことと、パラメータを1つしか渡すことができません。

	// In this example we show how to invoke a coroutine using a string name and stop it

	function Start () {
		StartCoroutine("DoSomething", 2.0);
		yield WaitForSeconds(1);
		StopCoroutine("DoSomething");
	}

	function DoSomething (someParameter : float) {
		while (true) {
			print("DoSomething Loop");
			// Yield execution of this coroutine and return to the main loop until next frame
			yield;
		}
	}
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;
        }
    }
}
import UnityEngine
import System.Collections

public class ExampleClass(MonoBehaviour):

	def Start() as IEnumerator:
		StartCoroutine('DoSomething', 2.0F)
		yield WaitForSeconds(1)
		StopCoroutine('DoSomething')

	def DoSomething(someParameter as float) as IEnumerator:
		while true:
			print('DoSomething Loop')
			yield