publicclassAsyncAwaitExample:MonoBehaviour{privateasyncvoidStart(){ Debug.Log("Wait.");awaitWaitOneSecondAsync(); DoMoreStuff();// Will not execute until WaitOneSecond has completed}privateasyncTaskWaitOneSecondAsync(){awaitTask.Delay(TimeSpan.FromSeconds(1)); Debug.Log("Finished waiting."); } ...
WaitWhile:当返回条件为假时才执行后续步骤. 协程的执行也会受到其他因素的影响,例如:当时间缩放值Time.timeScale被修改后,放大或者缩小.FixedUpdate 方法会受影响,则WaitForFixedUpdate也会跟着受影响;当Update方法中同步加载较大的对象时,WaitForSeconds所指定的时间就可能会与实际的时间不一致.所以在执行协程等待时,要...
= null && _current.MoveNext()) { //Return whatever the coroutine yielded, so we will yield the //same thing yield return _current.Current; } else //Otherwise wait for the next frame yield return null; } } IEnumerator CountUp() { //We have a local increment so the routines //get ...
... yield return Yielders.GetWaitForSeconds(1.0f); // wait for one second ... Coroutine 的工作原理 观察调用链可知,Unity Coroutine 的调用约定靠返回的 IEnumerator 对象来维系。我们知道 IEnumerator 的核心功能函数是: bool MoveNext(); 这个函数在每次被 Unity 协程调度函数 (通常是协程所在类的 Setup...
public class TimeWait : MonoBehaviour { // Use this for initialization void Start () { StartCoroutine(DelayTime()); Debug.Log("等待两秒"); } // Update is called once per frame void Update () { } IEnumerator DelayTime() { yield return new WaitForSeconds(2f); ...
using System.Collections; using System.Collections.Generic; using UnityEngine;// WaitForSeconds example. // // Create three cubes. Press the "Move cubes normally" button to animate them. // Press the "Move cubes quickly" button to animate them faster. // Have WaitForSeconds wait for different...
值得注意的是 WaitForSeconds()受Time.timeScale影响,当Time.timeScale = 0f 时,yield return new WaitForSecond(x) 将不会满足。 以上其实说了这么多,就记住一个要点:当执行到yield语句的时候,代码权限就被交给调用它的父线程了,就把你给挂起了,外部根据你yield的条件,知道该什么时候去调用MoveNext(),当再次调...
Debug.Log("Wait Stop"); }publicinterfaceIWait {boolTick(); }publicclassMyWaitForSeconds : IWait {float_seconds =0f;publicMyWaitForSeconds(floatseconds) { _seconds=seconds; }publicboolTick() { _second-=Time.deltaTime;return_seconds<=0; ...
publicclassDestroyTest:UnityEngine.MonoBehaviour{privateUnityEngine.GameObject _gameObject;privatevoidStart(){_gameObject=newUnityEngine.GameObject("test");StartCoroutine(DelayedDestroy());}System.Collections.IEnumeratorDelayedDestroy(){// cache WaitForSeconds to reusevarwaitOneSecond=newUnityEngine.WaitForSeconds...
yield return new WaitWhile(() => oneSecond>1);//等到某判断条件为假时 AsyncOperation async = SceneManager.LoadSceneAsync("SceneName");//暂停协程,异步场景加载 yield return async; // 等待场景加载完成 yield break;//直接终止,类似于return操作 ...