====== 076 コルーチン(StartCoroutine)の使い方 ====== ===== 概要 ===== コルーチンを使えば、シングルスレッド環境でマルチスレッド風な動作を実現します。通常の関数とは異なり処理を途中で中断したり任意のタイミングで再開できます。用途としては、シーン切替用に行うフェードインアウト処理で使うなどが考えられます。 ==== ソースコード ==== using UnityEngine; using System.Collections; public class TestScript : MonoBehaviour { void Start() { StartCoroutine( FirstCoroutine() ); } void Update() { Debug.Log("Update"); } IEnumerator FirstCoroutine() { Debug.Log("First 1"); yield return 0; Debug.Log("First 2"); yield return StartCoroutine(SecondCoroutine()); Debug.Log("First 3"); } IEnumerator SecondCoroutine() { Debug.Log("Second 1"); yield return 0; } } ==== 実行結果 ==== First 1 Update First 2 Second 1 First 3