您的位置:首页 > 移动开发 > Unity3D

UNITY 3D 协程执行顺序

2016-07-29 14:17 615 查看
一,
using UnityEngine;  
using System.Collections;  
  
public class TestCoroutine : MonoBehaviour {  
  
    private bool isStartCall = false;  //Makesure Update() and LateUpdate() Log only once  
    private bool isUpdateCall = false;  
    private bool isLateUpdateCall = false;  
    // Use this for initialization  
    void Start () {  
        if (!isStartCall)  
        {  
            Debug.Log("Start Call Begin");  
            StartCoroutine(StartCoutine());  
            Debug.Log("Start Call End");  
            isStartCall = true;  
        }  
      
    }  
    IEnumerator StartCoutine()  
    {  
          
        Debug.Log("This is Start Coroutine Call Before");  
        yield return new WaitForSeconds(1f);  
        Debug.Log("This is Start Coroutine Call After");  
             
    }  
    // Update is called once per frame  
    void Update () {  
        if (!isUpdateCall)  
        {  
            Debug.Log("Update Call Begin");  
            StartCoroutine(UpdateCoutine());  
            Debug.Log("Update Call End");  
            isUpdateCall = true;  
        }  
    }  
    IEnumerator UpdateCoutine()  
    {  
        Debug.Log("This is Update Coroutine Call Before");  
        yield return new WaitForSeconds(1f);  
        Debug.Log("This is Update Coroutine Call After");  
    }  
    void LateUpdate()  
    {  
        if (!isLateUpdateCall)  
        {  
            Debug.Log("LateUpdate Call Begin");  
            StartCoroutine(LateCoutine());  
            Debug.Log("LateUpdate Call End");  
            isLateUpdateCall = true;  
        }  
    }  
    IEnumerator LateCoutine()  
    {  
        Debug.Log("This is Late Coroutine Call Before");  
        yield return new WaitForSeconds(1f);  
        Debug.Log("This is Late Coroutine Call After");  
    }  
}  

 得到日志输入结果如下:



二,

using UnityEngine;  
using System.Collections;  
  
public class TestCoroutine : MonoBehaviour {  
  
    private bool isStartCall = false;  //Makesure Update() and LateUpdate() Log only once  
    private bool isUpdateCall = false;  
    private bool isLateUpdateCall = false;  
    // Use this for initialization  
    void Start () {  
        if (!isStartCall)  
        {  
            Debug.Log("Start Call Begin");  
            StartCoroutine(StartCoutine());  
            Debug.Log("Start Call End");  
            isStartCall = true;  
        }  
      
    }  
    IEnumerator StartCoutine()  
    {  
          
        Debug.Log("This is Start Coroutine Call Before");  
        yield return new WaitForSeconds(1f);  
        Debug.Log("This is Start Coroutine Call After");  
             
    }  
    // Update is called once per frame  
    void Update () {  
        if (!isUpdateCall)  
        {  
            Debug.Log("Update Call Begin");  
            StartCoroutine(UpdateCoutine());  
            Debug.Log("Update Call End");  
            isUpdateCall = true;  
            this.enabled = false;  
            //this.gameObject.SetActive(false);  
        }  
    }  
    IEnumerator UpdateCoutine()  
    {  
        Debug.Log("This is Update Coroutine Call Before");  
        yield return new WaitForSeconds(1f);  
        Debug.Log("This is Update Coroutine Call After");  
        yield return new WaitForSeconds(1f);  
        Debug.Log("This is Update Coroutine Call Second");  
    }  
    void LateUpdate()  
    {  
        if (!isLateUpdateCall)  
        {  
            Debug.Log("LateUpdate Call Begin");  
            StartCoroutine(LateCoutine());  
            Debug.Log("LateUpdate Call End");  
            isLateUpdateCall = true;  
  
        }  
    }      IEnumerator LateCoutine()  
    {  
        Debug.Log("This is Late Coroutine Call Before");  
        yield return null;  
        Debug.Log("This is Late Coroutine Call After");  
    }  
}  

 先在Update中调用 this.enabled = false; 得到的结果:



然后把 this.enabled = false; 注释掉,换成 this.gameObject.SetActive(false); 得到的结果如下:



       整理得到:通过设置MonoBehaviour脚本的enabled对协程是没有影响的,但如果 gameObject.SetActive(false) 则已经启动的协程则完全停止了,即使在Inspector把gameObject 激活还是没有继续执行。也就说协程虽然是在MonoBehvaviour启动的(StartCoroutine)但是协程函数的地位完全是跟MonoBehaviour是一个层次的,不受MonoBehaviour的状态影响,但跟MonoBehaviour脚本一样受gameObject
控制,也应该是和MonoBehaviour脚本一样每帧“轮询” yield 的条件是否满足。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  unity Unity 3D