奥东here......Unity中的协程

参考文章地址:

 http://dsqiu.iteye.com/blog/2029701

        定义:

                              A coroutine is a function that is executed partially and, presuming suitable conditions are met, will be resumed at some point in the future until its work is done.

                              即协程是一个分部执行,遇到条件(yield return 语句)会挂起,直到条件满足才会被唤醒继续执行后面的代码。

协程的作用一共有两点:

                         1  延时(等待)一段时间执行代码

                                2  等某个操作完成之后再执行后面的代码

                                总结起来就是一句话:控制代码在特定的时机执行

     执行的时机:

                                Unity在每一帧(Frame)都会去处理对象上的协程。Unity主要是在Update后去处理协程(检查协程的条件是否满足),但也有写特例

                                

                                

                                  

                    注意点:  

                                1  协程不是线程,也不是异步执行的,协程和 MonoBehaviour 中的 Update函数一样也是在MainThread中执行的。所以使用协程你不用考虑同步和锁的问题

                 示例代码:

public class TestCoroutine : MonoBehaviour
{

     //Makesure Update() and LateUpdate() Log only once
    private bool isUpdateCall = false;
    private bool isLateUpdateCall = false;
    private  int num = 1;//展示各个log打印的顺序
    // Use this for initialization
    void Start()
    {
            Debug.Log("Start Call Begin  "+num++);// 1
            StartCoroutine(StartCoutine());
            Debug.Log("Start Call End  " + num++);//3
    }
    IEnumerator StartCoutine()
    {

        Debug.Log("This is Start Coroutine Call Before " + num++);//2
        yield return new WaitForSeconds(1f);
        //在执行上面语句的同时,num从2变成了11 这个时间断内有Update()方法和LateUpdate()方法在执行
        Debug.Log("This is Start Coroutine Call After " + num++);//11

    }
    // Update is called once per frame
    void Update()
    {
        if (!isUpdateCall)
        {
            Debug.Log("Update Call Begin " + num++);//4
            StartCoroutine(UpdateCoutine());
            Debug.Log("Update Call End " + num++);//6
            isUpdateCall = true;
        }
    }
    IEnumerator UpdateCoutine()
    {
        Debug.Log("This is Update Coroutine Call Before " + num++);//5
        //yield return new WaitForSeconds(1f);
        yield return null;
        Debug.Log("This is Update Coroutine Call After " + num++);//10
    }
    void LateUpdate()
    {
        if (!isLateUpdateCall)
        {
            Debug.Log("LateUpdate Call Begin " + num++);//7
            StartCoroutine(LateCoutine());
            Debug.Log("LateUpdate Call End " + num++);//9
            isLateUpdateCall = true;
        }
    }
    IEnumerator LateCoutine()
    {
        Debug.Log("This is Late Coroutine Call Before " + num++);//8
        yield return new WaitForSeconds(1f);
        Debug.Log("This is Late Coroutine Call After " + num++);//12
    }
}

  

Pre Tip:

1   StartCoroutine()方法在执行的时候,对应绑定的物体上销毁的时候,不管StartCoroutine()方法执行到何处,都停止执行,这个时候就会出现问题

          解决办法: 物体(GameObject)本身只调用本身的StartCoroutine()方法,当别的物件A调用物件B的Coroutine()方法时,当关联的物件消失的时候,就会出现问题

posted @ 2014-07-02 12:50  奥东  阅读(232)  评论(0编辑  收藏  举报