在 Unity 中,你可以使用 yield return new WaitForSeconds(x)
来暂停一个协程。在这里,x
是你希望协程暂停的时间(以秒为单位)。例如:
using System.Collections;
using UnityEngine;
public class Example : MonoBehaviour
{
void Start()
{
StartCoroutine(ExampleCoroutine());
}
IEnumerator ExampleCoroutine()
{
Debug.Log("Started Coroutine at timestamp : " + Time.time);
// 协程暂停 2 秒
yield return new WaitForSeconds(2);
Debug.Log("Finished Coroutine at timestamp : " + Time.time);
}
}
这将在启动协程后 2 秒钟输出 "Finished Coroutine"。