https://docs.unity.cn/cn/current/ScriptReference/Coroutine.html
https://docs.unity.cn/cn/current/ScriptReference/YieldInstruction.html
https://docs.unity.cn/cn/current/ScriptReference/AsyncOperation.html
https://www.cnblogs.com/revoid/p/6663922.html
https://docs.unity.cn/cn/current/ScriptReference/WaitUntil.html
https://docs.unity.cn/cn/current/ScriptReference/WaitWhile.html
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Runtime.InteropServices;
namespace System.Collections
{
/// <summary>Supports a simple iteration over a nongeneric collection.</summary>
/// <filterpriority>1</filterpriority>
// Token: 0x02000011 RID: 17
[ComVisible(true)]
[Guid("496B0ABF-CDEE-11d3-88E8-00902754C43A")]
public interface IEnumerator
{
/// <summary>Advances the enumerator to the next element of the collection.</summary>
/// <returns>true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.</returns>
/// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created. </exception>
/// <filterpriority>2</filterpriority>
// Token: 0x060000A9 RID: 169
bool MoveNext();
/// <summary>Gets the current element in the collection.</summary>
/// <returns>The current element in the collection.</returns>
/// <exception cref="T:System.InvalidOperationException">The enumerator is positioned before the first element of the collection or after the last element.</exception>
/// <filterpriority>2</filterpriority>
// Token: 0x17000010 RID: 16
// (get) Token: 0x060000AA RID: 170
object Current { get; }
/// <summary>Sets the enumerator to its initial position, which is before the first element in the collection.</summary>
/// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created. </exception>
/// <filterpriority>2</filterpriority>
// Token: 0x060000AB RID: 171
void Reset();
}
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
public class CustomIEnumerator : IEnumerator {
public object Current { get; }
// true if the enumerator was successfully advanced to the next element;
// false if the enumerator has passed the end of the collection
public bool MoveNext() {
return false;
}
public void Reset() {
throw new System.NotImplementedException();
}
}
private IEnumerator IE_Test() {
Debug.Log("1");
// MoveNext() 返回 false 时, 才能执行后续语句
yield return new CustomIEnumerator();
Debug.Log("2");
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
private IEnumerator IE_Test() {
AsyncOperation ao = SceneManager.LoadSceneAsync("Game");
while(!ao.isDone) {
Debug.Log(ao.progress);
yield return null;
}
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
private IEnumerator IE_Test() {
yield return new WaitForEndOfFrame();
Debug.Log("AfterEndOfFrame");
// e.g. 2
Debug.Log(Time.frameCount);
yield return new WaitForFixedUpdate();
Debug.Log("AfterFixedUpdate");
// e.g. 3
Debug.Log(Time.frameCount);
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
private IEnumerator IE_Test() {
yield return new WaitForSeconds(5);
Debug.Log("After5Seconds");
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
private IEnumerator IE_Test() {
ResourceRequest rr = Resources.LoadAsync("Image");
while (!rr.isDone) {
Debug.Log(rr.progress);
yield return null;
}
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
private IEnumerator IE_Test() {
UnityWebRequest uwr = new UnityWebRequest("https://www.baidu.com");
UnityWebRequestAsyncOperation async = uwr.SendWebRequest();
yield return async;
Debug.Log(async.progress);
while (!async.isDone) {
Debug.Log(async.progress);
}
Debug.Log(async.isDone);
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
private IEnumerator IE_Test() {
WWW www = new WWW("");
AssetBundleRequest abr = www.assetBundle.LoadAssetAsync("");
yield return abr;
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
private IEnumerator IE_Test() {
AssetBundleCreateRequest abcr = AssetBundle.LoadFromFileAsync("");
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
private IEnumerator IE_Test() {
yield return new WaitForSeconds(1);
Debug.Log("WaitForSeconds");
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
private IEnumerator IE_Test() {
yield return new WaitUntil(Test);
Debug.Log("True");
}
bool Test() {
Debug.Log(Time.frameCount);
return Time.frameCount > 1000;
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
private IEnumerator IE_Test() {
yield return new WaitWhile(Test);
Debug.Log("False");
}
bool Test() {
Debug.Log(Time.frameCount);
return Time.frameCount < 1000;
}