阅读量:0
在C#中使用StartCoroutine来实现延时操作的技巧是通过创建一个协程函数来实现延时操作。以下是一个示例代码:
using System.Collections; using UnityEngine; public class DelayExample : MonoBehaviour { private void Start() { StartCoroutine(DelayedAction()); } private IEnumerator DelayedAction() { Debug.Log("Start of delayed action"); yield return new WaitForSeconds(2); Debug.Log("Delayed action completed"); } }
在上面的代码中,我们创建了一个DelayExample类,并在Start方法中调用StartCoroutine来启动一个协程函数DelayedAction。在DelayedAction函数中,我们使用yield return new WaitForSeconds来实现一个延时操作,这里延时2秒后打印"Delayed action completed"。这样就实现了在协程中进行延时操作的技巧。