这允许您执行以下操作: publicclassAsyncExample:MonoBehaviour{publicasyncvoidStart(){// Wait one secondawaitnewWaitForSeconds(1.0f);// Wait for IEnumerator to completeawaitCustomCoroutineAsync();awaitLoadModelAsync();// You can also get the final yielded value from the coroutinevarvalue=(string)(awai...
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."); } ...
//WaitForSecondsexample. // // Create three cubes. Press the "Move cubes normally" button to animate them. // Press the "Move cubes quickly" button to animate them faster. // HaveWaitForSecondswait for different amounts of time. The cubes // move upward or downward one at a time. ...
IEnumerator IMyWaitForSecond(float time) { Debug.Log("Wait Start"); yield return new MyWaitForSeconds(time); Debug.Log("Wait Stop"); } public interface IWait { bool Tick(); } public class MyWaitForSeconds : IWait { float _seconds = 0f; public MyWaitForSeconds(float seconds) { _seconds ...
publicclassDestroyTest:UnityEngine.MonoBehaviour{privateUnityEngine.GameObject _gameObject;privatevoidStart(){_gameObject=newUnityEngine.GameObject("test");StartCoroutine(DelayedDestroy());}System.Collections.IEnumeratorDelayedDestroy(){// cache WaitForSeconds to reusevarwaitOneSecond=newUnityEngine.WaitForSeconds...
yield return Yielders.GetWaitForSeconds(1.0f); // wait for one second ... 1. 2. 3. Coroutine 的工作原理 观察调用链可知,Unity Coroutine 的调用约定靠返回的IEnumerator对象来维系。我们知道IEnumerator的核心功能函数是: bool MoveNext(); 1.
值得注意的是 WaitForSeconds()受Time.timeScale影响,当Time.timeScale = 0f 时,yield return new WaitForSecond(x) 将不会满足。 以上其实说了这么多,就记住一个要点:当执行到yield语句的时候,代码权限就被交给调用它的父线程了,就把你给挂起了,外部根据你yield的条件,知道该什么时候去调用MoveNext(),当再次调...
//Otherwise wait for the next frame yield return null; } } IEnumerator CountUp() { //We have a local increment so the routines //get independently faster depending on how //long they have been active float increment = 0; while(true) ...
协程的缺点 这里我们列出了常见的三个缺点。我们知道Unity给我们提供的MonoBehavior这样一个脚本的基类,而...
// This will wait 1 second. Routine normalSpeed = Routine.Start( this, MyCoroutine() ); Routine halfSpeed = Routine.Start( this, MyCoroutine() ).SetTimeScale(0.5f); Routine doubleSpeed = Routine.Start( this, MyCoroutine() ).SetTimeScale(2); IEnumerator MyCoroutine() { // Any ...