协程的官方定义是一种具备暂停执行并将控制权返回给Unity,待下一帧时继续执行。通俗点讲就是,协程是一种能够分部执行的函数,即该函数不是每次调用时都会执行函数体内的所有方法,而是只调用其中部分代码。写到这里不知道您有没有发现,该定义有点像IEnumerator的延迟执行。举一个例子:php
void Start () { IEnumerator numbers = YieldThreeNumbers (); for (int i = 0; i < 3; i++) { if(!numbers.MoveNext()) break; Debug.Log((int)numbers.Current); } } IEnumerator YieldThreeNumbers() { yield return 1; yield return 2; yield return 3; }
结果:html
能够看到当咱们执行一次MoveNext方法,才会取得当前当前的值,因此须要循环调用MoveNext才能将所有值取出。ios
协程也是一样的方法:每一帧都会调用MoveNext方法,期间保存了下一次执行的位置,等到下一帧时会在该位置继续执行。web
PS: 在C#中,yield和IEnumerator一块儿使用时其实是实现了Itertor模式,详情可参考这篇文章。http://csharpindepth.com/articles/chapter6/iteratorblockimplementation.aspx多线程
由此也能够看到,协程其实与多线程一点关系都没有。协程是在主线程中执行的,且每次只能执行一个协程。app
首先咱们须要定义一个返回IEnumerator的方法,如:ide
IEnumerator GetEnumerator() { Yield return null; }
而后在调用StarCoroutine方法,其签名以下:函数
Coroutine StartCoroutine(string methodName,object value=null);性能
Coroutine StartCoroutine(IEnumerator routine);学习
在是使用第一个方法时,咱们直接将传入上面定义的方法名:
StartCoroutine(“GetEnumerator”);
注意该方法的参数是IEnumerator,因此咱们能够直接将调用上面定义的方法的返回值传入:
StartCoroutine(GetEnumertor());
下面看一个来自官网的例子:
IEnumerator Fade() { for (float f = 1f; f >= 0; f -= 0.1f) { Color c = renderer.material.color; //减小a值,即透明度 c.a = f; renderer.material.color = c; yield return null; } } void Update() { if (Input.GetKeyDown("f")) { //没按一次"f"键,gameObject的透明度都在减小,实现渐隐的效果 StartCoroutine("Fade"); }
固然,咱们不单单能够yield null,还能够yield其它的表达式:
1. 其它的数值,如0:
和null相似,不过不推荐。由于会存在装箱,拆箱的问题,或多或少会影响性能。
2. WaitForEndOfFrame
等待至全部的Camera和GUI都呈现好了以后执行。
3. WaitForFixedUpdate
等待至全部物理都计算后执行
4. WaitForSeconds
在指定时间段内不执行
5. WWW
等待一个web请求完成
6. 另外一个协程
这是比较有意思的一点。由于StartCoroutine的返回值是Coroutine,因此咱们能够yield另外一个协程。
void StopCoroutine(string methodName);
void StopCoroutine(IEnumerator routine);
其中StopCortouine(string methodName)只能中止由与之相对应的StarCoroutine(string methodName)启动的协程。
还有其它的方法来中止协程,但都不是中止某个指定的协程,而是中止多个协程。
void StopAllCoroutines()
中止该behavior内的所有协程
void SetActive(bool value);
将behavior的active设为false后,其内部的协程也都会中止。
协程能够将一个方法,放到多个帧内执行,在很大程度上提升了性能。但协程也是有缺陷的:
下面咱们来解决前三个问题,为协程添加返回值、异常处理和泛型。关于第四个问题的解决方式,请参考最下方的连接:
返回值:
public class ReturnValueCoroutine { private object result; public object Result { get {return result;} } public UnityEngine.Coroutine Coroutine; public IEnumerator InternalRoutine(IEnumerator coroutine) { while(true) { if(!coroutine.MoveNext()){ yield break; } object yielded = coroutine.Current; if(yielded != null){ result = yielded; yield break; } else{ yield return coroutine.Current; } } } } public class Demo : MonoBehaviour { IEnumerator Start () { ReturnValueCoroutine myCoroutine = new ReturnValueCoroutine (); myCoroutine.Coroutine = StartCoroutine (myCoroutine.InternalRoutine(TestNewRoutine())); yield return myCoroutine.Coroutine; Debug.Log (myCoroutine.Result); } IEnumerator TestNewRoutine() { yield return 10; } }
泛型:
public static class MonoBehaviorExt { public static Coroutine<T> StartCoroutine<T>(this MonoBehaviour obj, IEnumerator coroutine){ Coroutine<T> coroutineObject = new Coroutine<T>(); coroutineObject.coroutine = obj.StartCoroutine(coroutineObject.InternalRoutine(coroutine)); return coroutineObject; } } public class Coroutine<T>{ private T result; public T Result { get {return result;} } public Coroutine coroutine; public IEnumerator InternalRoutine(IEnumerator coroutine){ while(true){ if(!coroutine.MoveNext()){ yield break; } object yielded = coroutine.Current; if(yielded != null && yielded.GetType() == typeof(T)){ result = (T)yielded; yield break; } else{ yield return coroutine.Current; } } } } public class Demo : MonoBehaviour { Coroutine<int> routine; IEnumerator Start () { routine = this.StartCoroutine<int>(TestNewRoutine()); //Start our new routine yield return routine; // wait as we normally can } IEnumerator TestNewRoutine(){ yield return null; yield return new WaitForSeconds(2f); yield return 10; } void Update() { //由于延时,因此要等待一段时间才能显示 Debug.Log(routine.Result); } }
异常处理:
public static class MonoBehaviorExt { public static Coroutine<T> StartCoroutine<T>(this MonoBehaviour obj, IEnumerator coroutine){ Coroutine<T> coroutineObject = new Coroutine<T>(); coroutineObject.coroutine = obj.StartCoroutine(coroutineObject.InternalRoutine(coroutine)); return coroutineObject; } } public class Coroutine<T>{ public T Result { get{ if(e != null){ throw e; } return result; } } private T result; private Exception e; public UnityEngine.Coroutine coroutine; public IEnumerator InternalRoutine(IEnumerator coroutine){ while(true){ try{ if(!coroutine.MoveNext()){ yield break; } } catch(Exception e){ this.e = e; yield break; } object yielded = coroutine.Current; if(yielded != null && yielded.GetType() == typeof(T)){ result = (T)yielded; yield break; } else{ yield return coroutine.Current; } } } } public class Demo : MonoBehaviour { IEnumerator Start () { var routine = this.StartCoroutine<int>(TestNewRoutineGivesException()); yield return routine.coroutine; try{ Debug.Log(routine.Result); } catch(Exception e){ Debug.Log(e.Message); // do something Debug.Break(); } } IEnumerator TestNewRoutineGivesException(){ yield return null; yield return new WaitForSeconds(2f); throw new Exception("Bad thing!"); } }
1. 使用lamada表达式接受返回值:http://answers.unity3d.com/questions/207733/can-coroutines-return-a-value.html
2. Wrapping Unity C# Coroutines for Exception Handling, Value Retrieval, and Locking:http://zingweb.com/blog/2013/02/05/unity-coroutine-wrapper/
3. CoroutineScheduler:http://wiki.unity3d.com/index.php?title=CoroutineScheduler
以上是本人的学习成果及平时收集的资料,若是您有其它更好的资源或是想法,请怒砸至评论区,多多益善!
参考资料:Coroutines – More than you want to know,http://twistedoakstudios.com/blog/Post83_coroutines-more-than-you-want-to-know