1. 協同程序 (Coroutine) 簡介

協同程序是 Unity 中一種特殊的功能,允許你暫停函式的執行並在之後的幀中繼續。這對於需要在多個幀之間分散執行的操作特別有用。

2. 協同程序的主要用途

3. 使用協同程序的步驟

4. 協同程序的語法

using System.Collections;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    private IEnumerator ExampleCoroutine()
    {
        Debug.Log("協同程序開始");
        yield return new WaitForSeconds(2f);
        Debug.Log("等待 2 秒後繼續執行");
    }

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

5. yield 語句的類型