首先:Unity里面的协程并不是线程,协程是在unity主线程中运行的,每一帧中处理一次,而并不与主线程并行。这就意味着在协程之间并不存在着所谓线程间的同步和互斥问题。要说协程的不足就是不能运用处理器的多核来提高处理性能,毕竟这个在运行时事实上是在一个线程中执行的。
附上一张协程运行图:
协程的跳出由yield return控制,yield return后面可以跟的表达式:
a) null - the coroutine executes the next time that it is eligible
b) WaitForEndOfFrame - the coroutine executes on the frame, after all of the rendering and GUI is complete
c) WaitForFixedUpdate - causes this coroutine to execute at the next physics step, after all physics is calculated
d) WaitForSeconds - causes the coroutine not to execute for a given game time period
e) WWW - waits for a web request to complete (resumes as if WaitForSeconds or null)
f) Another coroutine - in which case the new coroutine will run to completion before the yielder is resumed
注意:不能在Update或FixedUpdate函数内使用协程序。
下面附上一个用协程实现对话慢慢跳出的代码:
using UnityEngine;
using System.Collections;
public class dialog_yield : MonoBehaviour {
public string dialogStr = "yield return的作用是在执行到这行代码之后,将控制权立即交还给外部。yield return之后的代码会在外部代码再次调用MoveNext时才会执行,直到下一个yield return——或是迭代结束。虽然上面的代码看似有个死循环,但事实上在循环内部我们始终会把控制权交还给外部,这就由外部来决定何时中止这次迭代。有了yield之后,我们便可以利用“死循环”,我们可以写出含义明确的“无限的”斐波那契数列。";
public float speed = 5.0f;
// Use this for initialization
void Start () {
StartCoroutine(ShowDialog());
}
// Update is called once per frame
void Update () {
}
IEnumerator ShowDialog(){
float timeSum = 0.0f;
while(guiText.text.Length < dialogStr.Length){
timeSum += speed * Time.deltaTime;
guiText.text = dialogStr.Substring(0, System.Convert.ToInt32(timeSum));
yield return null;
}
}
}
协程的控制:
开启:使用MonoBehaviour.StartCoroutine方法即可开启一个协同程序,也就是说该方法必须在MonoBehaviour或继承于MonoBehaviour的类中调用。使用StartCoroutine(string methodName)和StartCoroutine(IEnumerator routine)都可以开启一个线程。区别在于使用字符串作为参数可以开启线程并在线程结束前终止线程,相反使用IEnumerator 作为参数只能等待线程的结束而不能随时终止(除非使用StopAllCoroutines()方法);另外使用字符串作为参数时,开启线程时最多只能传递一个参数,并且性能消耗会更大一点,而使用IEnumerator 作为参数则没有这个限制。
终止:使用StopCoroutine(string methodName)来终止一个协同程序,使用StopAllCoroutines()来终止所有可以终止的协同程序,但这两个方法都只能终止该MonoBehaviour中的协同程序。还有一种方法可以终止协同程序,即将协同程序所在gameobject的active属性设置为false,当再次设置active为ture时,协同程序并不会再开启;如是将协同程序所在脚本的enabled设置为false则不会生效。这是因为协同程序被开启后作为一个线程在运行,而MonoBehaviour也是一个线程,他们成为互不干扰的模块,除非代码中用调用,他们共同作用于同一个对象,只有当对象不可见才能同时终止这两个线程。然而,为了管理我们额外开启的线程,Unity3D将协同程序的调用放在了MonoBehaviour中,这样我们在编程时就可以方便的调用指定脚本中的协同程序,而不是无法去管理,特别是对于只根据方法名来判断线程的方式在多人开发中很容易出错,这样的设计保证了对象、脚本的条理化管理,并防止了重名。