Coroutine 复习

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

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();
    }
}
IEnumerator
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");
}
CustomIEnumerator

private IEnumerator IE_Test() {
    AsyncOperation ao = SceneManager.LoadSceneAsync("Game");
    while(!ao.isDone) {
        Debug.Log(ao.progress);
        yield return null;
    }
}
AsyncOperation
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);
}
WaitForEndOfFrame&WaitForFixedUpdate
private IEnumerator IE_Test() {
    yield return new WaitForSeconds(5);
    Debug.Log("After5Seconds");
}
WaitForSeconds
private IEnumerator IE_Test() {
    ResourceRequest rr = Resources.LoadAsync("Image");
    while (!rr.isDone) {
        Debug.Log(rr.progress);
        yield return null;
    }
}
ResourceRequest
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);
}
UnityWebRequestAsyncOperation
private IEnumerator IE_Test() {
    WWW www = new WWW("");
    AssetBundleRequest abr = www.assetBundle.LoadAssetAsync("");
    yield return abr;
}
AssetBundleRequest
private IEnumerator IE_Test() {
    AssetBundleCreateRequest abcr = AssetBundle.LoadFromFileAsync("");
}
AssetBundleCreateRequest
private IEnumerator IE_Test() {
    yield return new WaitForSeconds(1);
    Debug.Log("WaitForSeconds");
}
WaitForSeconds
private IEnumerator IE_Test() {

    yield return new WaitUntil(Test);
    Debug.Log("True");
}

bool Test() {
    Debug.Log(Time.frameCount);
    return Time.frameCount > 1000;
}
WaitUntil
private IEnumerator IE_Test() {

    yield return new WaitWhile(Test);
    Debug.Log("False");
}

bool Test() {
    Debug.Log(Time.frameCount);
    return Time.frameCount < 1000;
}
WaitWhile
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值