协程使用IEnumerator的几种方式

Unity 使用StartCoroutine(IEnumerator)来启动一个协程。参数IEnumerator对象,通常有三种方式获得。

  • 第一种方式,也是最常用方式,是使用带有yield指令的协程函数。

    private IEnumerator Start()
    {
        yield return null;
    }
    

    解读一下这个yield return的几种情况:

    • return StartCoroutine 是等待返回的Coroutine结束。
    • return YieldInstruction 子类(就是各种系统提供的WaitFor开头的对象),等待特定时间或事件结束。
    • return CustomYieldInstruction子类,包括系统提供的Wait开头的函数,和自己继承扩展的,等待特定时间或事件结束。
    • return IEnumerator自己定义实现类,等待到自定义的时间或事件结束。
    • return 其它情况的对象,都是作为协程的返回值,绑定到Current属性上,等待一帧的时间。
  • 第二种方式,继承Unity提供的类CustomYieldInstruction,但其实CustomYieldInstruction是实现了IEnumerator。

  • 第三种方式,就是自己实现IEnumerator接口,手动new出一个IEnumerator接口实现类。(后面测试会用到这个类)

    // 等待60帧
    public class MyWait : IEnumerator
    {
        private int frame = 0;
        
        // 对应协程运行时,当前上下文的对象。
        // 比如指令 yield return object; 返回的值。
        public object Current { get { return null; } }
        
        // 判定协程是否运行结束
        public bool MoveNext() 
        { 
            if (++this.frame < 60)
            {
                return true;
            }
            else 
            {
                return false;
            }
        }
    
        public void Reset() 
        { 
            this.frame = 0; 
        }
    }
    

这个IEnumerator代表的是一个Routine,叫做例程。不同Routine之间协同执行,就是Coroutine协程。这个Routine需要能够分布计算,才能够互相协作,不然一路执行到底,就是一般函数了。而IEnumerator接口恰恰承担了这个分布计算的任务。每次执行就是一次MoveNext(),并且可以通过Current返回执行中的结果。

所以,带有yield指令的IEnumerator的函数,最终会被编译成一个实现了IEnumerator接口的类,这是C#自带的功能。

另外,还有系统提供的继承自类YieldInstruction的内置指令是不可扩展的,这是Unity特殊处理的类型,和IEnumerator没有关系。但实现原理,就是简单的定时回调,并不像IEnumerator代表的例程,可以承载复杂的逻辑分布。

接下来,就使用各种测试,来了解不同IEnumerator对于协程的使用有什么不同,使用的Unity版本是2017.2 of 3。

  • 使用MonoBehaviour的IEnumerator Start()来做启动测试。先看一个YieldInstruction,简单的等待两次1秒。

    private IEnumerator Start()
    {
        yield return new WaitForSeconds(1.0f);
        Debug.Log("wait one seconds");
    
        yield return new WaitForSeconds(1.0f);
        Debug.Log("wait one seconds");
    }
    
  • 缓存YieldInstruction,不必每次new,也是可以的。

    private IEnumerator Start()
    {
        var w = new WaitForSeconds(1.0f);
    
        yield return w;
        Debug.Log("wait one seconds");
    
        yield return w;
        Debug.Log("wait one seconds");
    }
    
  • yield 等待有YieldInstruction的协程函数,使用StartCoroutine。

    private IEnumerator Start()
    {
        yield return this.StartCoroutine(Test());
        Debug.Log("wait one seconds");
    
        yield return this.StartCoroutine(Test());
        Debug.Log("wait one seconds");
    }
    
    private IEnumerator Test()
    {
        yield return new WaitForSeconds(1.0f);
    }
    
  • yield 等待有YieldInstruction的协程函数,不使用StartCoroutine,也是一样的。

    private IEnumerator Start()
    {
        yield return Test();
        Debug.Log("wait one seconds");
    
        yield return Test();
        Debug.Log("wait one seconds");
    }
    
    private IEnumerator Test()
    {
        yield return new WaitForSeconds(1.0f);
    }
    
  • yield 等待自定义IEnumerator,以下两种形式是一样的。

    private IEnumerator Start()
    {
        yield return Test();
        Debug.Log("wait one seconds");
    
        yield return Test();
        Debug.Log("wait one seconds");
    }
    
    // 协程函数
    private IEnumerator Test()
    {
        yield return new MyWait();
    }
    
    // 普通函数
    private IEnumerator Test()
    {
        return new MyWait();
    }
    
  • 直接 yield 等待自定义IEnumerator。

    private IEnumerator Start()
    {
        yield return new MyWait();
        Debug.Log("wait one seconds");
    
        yield return new MyWait();
        Debug.Log("wait one seconds");
    }
    
  • 缓存自定义IEnumerator对象,缓存对象需要手动Reset()才能继续有效使用。

    private IEnumerator Start()
    {
        var w = new MyWait();
    
        yield return w;
        Debug.Log("wait one seconds");
        
        // 等待无效
        yield return w;
        Debug.Log("wait one seconds");
    }
    
    private IEnumerator Start()
    {
        var w = new MyWait();
    
        yield return w;
        Debug.Log("wait one seconds");
        
        w.Reset();
        
        // 等待有效
        yield return w;
        Debug.Log("wait one seconds");
    }
    
    private IEnumerator Start()
    {
        var w = new MyWait();
    
        yield return this.StartCoroutine(w);
        Debug.Log("wait one seconds");
    
        // 没有w.Reset()等待无效
    
        yield return this.StartCoroutine(w);
        Debug.Log("wait one seconds");
    }
    
  • 缓存协程函数,非第一次等待无效,并且Rest()运行时异常,NotSupportedException。

    private IEnumerator Test()
    {
        yield return new MyWait();
    }
    
    private IEnumerator Start()
    {
        var w = Test();
    
        yield return w;
        Debug.Log("wait one seconds");
        
        // w.Rest() 运行时异常
    
        // 等待无效
        yield return w;
        Debug.Log("wait one seconds");
    }
    
    private IEnumerator Start()
    {
        yield return Test();
        Debug.Log("wait one seconds");
        
        // 等待有效
        yield return Test();
        Debug.Log("wait one seconds");
    }
    

总结一下协程与IEnumerator

  • 协程函数与自定义IEnumerator,可以不需要StartCoroutine,直接yield return,如果StartCoroutine也无妨,效果一样。
  • 自定义IEnumerator缓存重复使用,需要手动调用Reset()。
  • 协程函数不能缓存使用,手动Reset()抛出异常。
  • CustomYieldInstruction 子类应该和自定义IEnumerator一致,不过我没测。
  • StartCoroutine绑定了IEnumerator对象,所以只要是同一个IEnumerator对象,多个StartCoroutine调用,相当于缓存了IEnumerator重复使用。
  • 直接yield return IEnumerator,unity应该自己调用了StartCoroutine,也就是自己绑定了一个Coroutine。

关于StopCoroutine

主要关注两个接口,StopCoroutine(Coroutine) 和 StopCoroutine(IEnumerator)。

private Coroutine   c;
private IEnumerator w;

private IEnumerator Test()
{
    yield return new WaitForSeconds(1.0f);
}

// 对应 StopCoroutine(w) 有效, 而 StopCoroutine(Test()) 无效
private IEnumerator Start()
{
    Debug.Log("start");
    w = Test();

    yield return w;
    Debug.Log("after one seconds");
}

// 对应 StopCoroutine(c) 有效
private IEnumerator Start()
{
    Debug.Log("start");
    c = this.StartCoroutine(Test());

    yield return c;
    Debug.Log("after one seconds");
}

// StopCoroutine(c) 和 StopCoroutine(w) 都有效
private IEnumerator Start()
{
    Debug.Log("start");
    w = Test();
    c = this.StartCoroutine(w);

    yield return c;
    Debug.Log("after one seconds");
}

private IEnumerator Start()
{
    Debug.Log("start");
    w = Test();
    c = this.StartCoroutine(w);
    
    // 等待无效, w 被 StartCoroutine 使用了。
    yield return w;
    Debug.Log("after one seconds");
}

所以,StartCoroutine(IEnumerator)是把 IEnumerator绑定到了Coroutine对象上,实际操作的还是IEnumerator对象。这时候,stop IEnumerator 或 Coroutine 对象是一样的。

而直接yield return IEnumerator,我们拿不到这个Coroutine,只能用IEnumerator来stop。



作者:scottcgi
链接:https://www.jianshu.com/p/857da3764d48
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
posted @ 2018-09-07 23:38  何人之名  阅读(1058)  评论(0编辑  收藏  举报