こんにちは、今日はUnityでコルーチンの使い方についてまとめていきます。
コルーチンは関数の実行途中で中断し、指定時間経過後等の条件で再開できる関数のようなものです。
詳細については公式マニュアルの以下ページにて説明がされています。
https://docs.unity3d.com/ja/2018.4/Manual/Coroutines.html
Table of Contents
確認環境
Unity 2018.4.15f1
コルーチンの書き方
以下のようにIEnumeratorを返す関数を定義し、その関数をStartCoroutineで実行します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | using System.Collections; using UnityEngine; public class TestScript : MonoBehaviour { void Start() { StartCoroutine( "CoroutineTest" ); } IEnumerator CoroutineTest() { Debug.Log( "CoroutineTest Start" ); yield return new WaitForSeconds(5.0f); Debug.Log( "CoroutineTest End" ); } } |
この例ではCoroutineTestの実行が開始され1つ目のログが出力された後、5秒後に2つ目のログが出力されます。
このように、通常は全ての処理が実行される関数内に、時間差で行いたい処理をまとめて記述することも可能です。
WaitForSeconds
https://docs.unity3d.com/ja/2018.4/ScriptReference/WaitForSeconds.html
前述の例でも使用したWaitForSecondsでは、指定秒数の間いコルーチンの処理を止めることができます。
1 2 3 4 5 6 | IEnumerator CoroutineTest() { Debug.Log( "CoroutineTest Start" ); yield return new WaitForSeconds(5.0f); Debug.Log( "CoroutineTest End" ); } |
WaitForSecondsRealtime
https://docs.unity3d.com/ja/2018.4/ScriptReference/WaitForSecondsRealtime.html
WaitForSecondsと同様指定した秒数処理を停止しますが、WaitForSecondsRealtimeではスケール化されていない時間を使用します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | using System.Collections; using UnityEngine; public class TestScript : MonoBehaviour { void Start() { StartCoroutine( "CoroutineTest" ); } IEnumerator CoroutineTest() { Debug.Log( "CoroutineTest Start" ); yield return new WaitForSecondsRealtime(5.0f); Debug.Log( "CoroutineTest End" ); } } |
WaitUntil
https://docs.unity3d.com/ja/2018.4/ScriptReference/WaitUntil.html
デリゲートを指定し、そのデリゲートがtrueと判定されるまで処理を停止します。
以下の例ではUpdate関数で変数countをインクリメントしcountの値が10になった時にデリゲートの条件がtrueとなり、処理が再開されます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | using System.Collections; using UnityEngine; public class TestScript : MonoBehaviour { int count; void Start() { StartCoroutine( "CoroutineTest" ); } IEnumerator CoroutineTest() { Debug.Log( "CoroutineTest Start" ); yield return new WaitUntil(() => count >= 10); Debug.Log( "CoroutineTest End" ); } void Update() { if (count < 10) { Debug.Log( "count : " + count); count++; } } } |
WaitWhile
https://docs.unity3d.com/ja/2018.4/ScriptReference/WaitWhile.html
デリゲートを指定し、そのデリゲートがfalseと判定されるまで処理を停止します。
以下の例ではUpdate関数で変数countをインクリメントしcountの値が10になった時にデリゲートの条件がfalseとなり、処理が再開されます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | using System.Collections; using UnityEngine; public class TestScript : MonoBehaviour { int count; void Start() { StartCoroutine( "CoroutineTest" ); } IEnumerator CoroutineTest() { Debug.Log( "CoroutineTest Start" ); yield return new WaitWhile(() => count < 10); Debug.Log( "CoroutineTest End" ); } void Update() { if (count < 10) { Debug.Log( "count : " + count); count++; } } } |
WaitForFixedUpdate
FixedUpdate関数が呼び出されるまで処理を停止します。
FixedUpdate
https://docs.unity3d.com/ja/current/ScriptReference/MonoBehaviour.FixedUpdate.html
https://docs.unity3d.com/ja/2018.4/ScriptReference/WaitForFixedUpdate.html
WaitForEndOfFrame
スクリーン上のレンダリング完了まで処理を停止します。
https://docs.unity3d.com/ja/2018.4/ScriptReference/WaitForEndOfFrame.html