Unity学習メモ - コルーチンの開始、停止、API

StartCoroutine はコルーチンを開始します。

yield ステートメントを使用すると、いつでもコルーチンの実行を一時停止できます。

コルーチンを実行状態に設定するには、  StartCoroutine 関数を使用する必要があります。

これは、スクリプトが無効になってからアクティブになった後、または接続されたオブジェクトがアクティブになった後にトリガーできます。

公式マニュアルリンク      UnityEngine.WaitForEndOfFrame - Unity Script API    コルーチン - Unity マニュアル

启用方式
        StartCoroutine(Test_00());
        StartCoroutine("Test_01");
        StartCoroutine(Test_02(5, 9));

============================================================

    private IEnumerator Test_00()

    private IEnumerator Test_01()

    private IEnumerator Test_02(int a, int b)

コルーチンを停止する

//停止指定的协程用这个
StopCoroutine("Test_01");
//这个是停止当前脚本的所有协程
StopAllCoroutines();

機能 1、WaitForSeconds

     コードは公式マニュアルからのものです

公式説明: スケーリング時間を使用して、指定された秒数の間コルーチンの実行を一時停止します。

using UnityEngine;
using System.Collections;

public class WaitForSecondsExample : MonoBehaviour
{
    void Start()
    {
        //启动我们在下面定义的名为ExampleCoroutine的协程。
        StartCoroutine(ExampleCoroutine());
    }

    IEnumerator ExampleCoroutine()
    {
        //打印函数第一次调用的时间。
        Debug.Log("Started Coroutine at timestamp : " + Time.time);

        //生成一个等待5秒的yield指令。
        yield return new WaitForSeconds(5);

        //等待5秒后再次打印时间。
        Debug.Log("Finished Coroutine at timestamp : " + Time.time);
    }
}

機能 2、WaitForSecondsRealtime

使い方は上記と同様ですが、時間間隔は上記より少し短くなります。

公式説明: スケールされていない時間を使用して、指定された秒数の間コルーチンの実行を一時停止します。

using UnityEngine;
using System.Collections;

public class WaitForSecondsExample : MonoBehaviour
{
    void Start()
    {
        StartCoroutine(Example());
    }

    IEnumerator Example()
    {
        print(Time.time);
        yield return new WaitForSecondsRealtime(5);
        print(Time.time);
    }
}

機能 3、WaitUntil

公式説明: 提供されたデリゲートが /true/ と評価されるまでコルーチンの実行を一時停止します。

WaitUntil(Func<bool> predicate); 使用する場合はWaitUntil(()=》variable)と書きます。

例えば、 yield return new WaitUntil(() => b); bool変数や判定条件を記述できます

using UnityEngine;
using System.Collections;

public class WaitUntilExample : MonoBehaviour
{
    public int frame;

    void Start()
    {
        StartCoroutine(Example());
    }

    IEnumerator Example()
    {
        Debug.Log("Waiting for princess to be rescued...");
        //WaitUntil(() => frame >= 10)为true时,继续执行。
        yield return new WaitUntil(() => frame >= 10);
        Debug.Log("Princess was rescued!");
    }

    void Update()
    {
        if (frame <= 10)
        {
            Debug.Log("Frame: " + frame);
            frame++;
        }
    }
}

機能 4、Waitwhile

公式説明: 提供されたデリゲートが /false/ と評価されるまでコルーチンの実行を一時停止します。

同上

using UnityEngine;
using System.Collections;

public class WaitWhileExample : MonoBehaviour
{
    public int frame;

    void Start()
    {
        StartCoroutine(Example());
    }

    IEnumerator Example()
    {
        Debug.Log("Waiting for prince/princess to rescue me...");
        //条件为false时,才会继续执行下一句
        yield return new WaitWhile(() => frame < 10);
        Debug.Log("Finally I have been rescued!");
    }

    void Update()
    {
        if (frame <= 10)
        {
            Debug.Log("Frame: " + frame);
            frame++;
        }
    }
}

おすすめ

転載: blog.csdn.net/m0_73841296/article/details/131183469