Unityのコルーチン(IEnumerator)の簡単な使い方

2020/12/09
カテゴリー: Unity


準備

今回はコルーチンの簡単な使い方の説明にImageオブジェクトを使います。
Imageオブジェクト一つとButtonオブジェクトを5つ用意します。


これから、それぞれのボタンを押したときの処理をスクリプトに書いていきます。

コルーチンの1番簡単(単純)な使い方

コルーチンの一番簡単な使い方としてWaitForSecondsを使った実行例を紹介します。
処理の内容としてはImageオブジェクトの色を赤→(1秒待つ)→緑といった感じです。
public void OnButton1Click()
{
    StartCoroutine(Processing());

    IEnumerator Processing()
    {
        GetComponent<Image>().color = new Color(1, 0, 0);
        yield return new WaitForSeconds(1);
        GetComponent<Image>().color = new Color(0, 1, 0);
    }
} 

流れは、IEnumerator型のProcessingという関数をStartCoroutine関数によって呼び出します。
Processing関数の中でGetComponent<Image>().color = new Color(1, 0, 0);によって赤に変え、
yield return new WaitForSeconds(1);によって処理を1秒待ち、
GetComponent<Image>().color = new Color(0, 1, 0);によって緑に変える
という感じです。

実行例がこちら


yield returnを複数回使う

yield returnはIEnumeratorの中で複数回使うことができます。
内容はImageオブジェクトの色を赤→(1秒待つ)→緑→(1秒待つ)→青といった感じです。
public void OnButton2Click()
{
    StartCoroutine(Processing());

    IEnumerator Processing()
    {
        GetComponent<Image>().color = new Color(1, 0, 0);
        yield return new WaitForSeconds(1);
        GetComponent<Image>().color = new Color(0, 1, 0);
        yield return new WaitForSeconds(1);
        GetComponent<Image>().color = new Color(0, 0, 1);
    }
}



実行例がこちら


別の関数の処理の終わりを待つ

コルーチンではWaitForSecondsだけでなく、別の関数の処理の終わりを待つこともできます。
public void OnButton3Click()
{
    GetComponent<Image>().color = new Color(1, 1, 1);

    StartCoroutine(Processing());


    IEnumerator Processing()
    {
        yield return SubProcessing();
        GetComponent<Image>().color = new Color(0, 0, 1);

    }

    IEnumerator SubProcessing()
    {
        for (int i = 0; i < 3; i++)
        {
            GetComponent<Image>().color -= new Color(0, 0.333f, 0.333f, 0);
            yield return new WaitForSeconds(0.5f);
        }
    }
}

まずProcessing関数を呼び出します。
Processing関数はSubProcessing関数の終了を待ち、終わったらImageオブジェクトの色を青に変えます。
SubProcessing関数はImageオブジェクトの色を0.5秒ごとに赤に近づける処理です。

実行例がこちら


特定のコルーチンの処理を止める

コルーチンの処理を途中で止めることができます。
public void OnButton4Click()
{
    StartCoroutine(Processing());

    IEnumerator Processing()
    {
        var col = StartCoroutine(SubProcessing());
        yield return new WaitForSeconds(5);
        StopCoroutine(col);
        GetComponent<Image>().color = new Color(0, 1, 0);

        IEnumerator SubProcessing()
        {
            while (true)
            {
                GetComponent<Image>().color = new Color(1, 0, 0);
                yield return new WaitForSeconds(1);
                GetComponent<Image>().color = new Color(0, 0, 1);
                yield return new WaitForSeconds(1);
            }
        }
    }

}

Processing関数の中で、SubProcessing関数を呼び出すと同時にcol変数の中に入れます。
5秒後StopCoroutineによってcol(SubProcessing)を停止しています。
SubProcessingではwhileによってずっと繰り返す処理を書いています。

実行例がこちら


すべてのコルーチンを止める

現在実行中のすべてのコルーチンを止めることができます。
public void OnButton5Click()
{
    StopAllCoroutines();
    GetComponent<Image>().color = new Color(0, 0, 0);
}

StopAllCoroutines()を呼び出すだけですべてのコルーチンを止めることができます。

実行例がこちら


このように意外と簡単にコルーチンを実装することができます。
何か処理を遅らせたり、止めたり、非同期にしたりしたい時に是非使ってみてください!