协程
一:什么是协同程序。
协同程序,即主程序在运行的同时开启另外一段处理逻辑,类似于开启一个线程。
注意:必须在MonoBehaviour
两种开启方法:
I:StartCoroutine(string methodName);
最多只能传递一个参数,比较消耗性能。
销毁:开启线程并在线程结束前终止线程(自己销毁本身)
II:StartCoroutine(IEnumerator routinue);
只能等待线程的终止而不能随时终止
销毁:StopAllStoroutines(),
协同程序所在的gameObject 的 active属性设置为false,再次设置active为true,协同程序将不会再次开启。
如果将协同程序所在的脚本的enable设置为false则不会消失。这是因为协同程序被开启后作为一个线程在运行,
而MonoBehaviour也是一个线程,他们成为互不干扰的的模块。他们公用一个对象,只有在这个对象不可见才能同时终止这两个线程。然而为了
管理我们额外开启的线程,Unity3D将协同程序的调用放在了MonoBehaviour中,这样在编程时就可以方便的调用指定的脚本中的协同程序,而不是
无法去管理,特别是对于只根据方法名来判断线程的方式在多人开发中很容易出错,这样的设计是为了保证对象,脚本的条件管理,并防止重名。
yield :代表先执行完本局语句(不管时间多长),或者执行完本yield方法调用,才执行后面的语句。
看下面的例子,数字代表着执行的顺序
private void Awake()
{
Debug.Log("11");
StartCoroutine(DoTest());
Debug.Log("333");
}
// Use this for initialization
void Start()
{
Debug.Log("444");
} IEnumerator DoTest()
{
Debug.Log("222");
yield return new WaitForSeconds(1.0f);
Debug.Log("555");
}
void Awake()
{
Debug.Log("");
StartCoroutine(StartTest());
Debug.Log("");
}
IEnumerator StartTest()
{
yield return StartCoroutine("Do");
Debug.Log("");
} IEnumerator Do()
{
Debug.Log("1 Now");
yield return null;
Debug.Log("");
}
理解:协程不是线程,也不是异步执行(知道就行)。
1.协程和MonoBehaviour的Update函数一样,也是在MainThread中执行的(一定得明白这句话意思)。
void Start () {
StartCoroutine(HelloCoroutine());
} void Update () {
Debug.Log("Update...");
}
void LateUpdate()
{
Debug.Log("LateUpdate...");
}
IEnumerator HelloCoroutine()
{
while (true)
{
Debug.Log("Coroutine...");
yield return null;
}
}
IEnumerator Count()
{
int seconds = 0;
while (true)
{
for (float timer = 0; timer < 2; timer += Time.deltaTime)
yield return 0; seconds++;
Debug.Log(seconds + " seconds have passed since the Coroutine started.");
}
}
yiled return null 等同于 yield return 0
我这边的理解是,停止正在执行的方法,并从下一帧开始执行(一般是0.02秒,与Update的每一帧是一样的,具体看Unity设置的timer)。
4.协程是可以传递参数的。
5.协程还可以嵌套协程。
IEnumerator HelloCoroutinue()
{
Debug.Log("Start----");
yield return StartCoroutine(Wait(0.2f)); // yield return new WaitForSeconds(0.2f);最终达到的效果是一样的。
Debug.Log("End----");
}
IEnumerator Wait(float s)
{
for(float timer =0;timer< s;timer+=Time.deltaTime)
{
Debug.Log("当前 timer" + timer.ToString());
yield return 0; // yield return null;
}
Debug.Log("Wait.....");
}
看截图中画线的时间差,再次验证了与Update好像。暂停的时间都是一样的。
可以看到暂停了当前的方法去执行yield return后的方法。
补充注意:
a.多个协程可以同时运行,它们会根据各自的启动顺序来更新;
b.如果你想让多个脚本访问一个协程,可以定义为静态的协程;
(这篇博客是参考网上某篇博客,自己简化,加入了一点点自己的理解)