unity 协成的执行顺序

来源:互联网 发布:java try是干什么的 编辑:程序博客网 时间:2024/06/02 18:50

刚接触c#的协成内容,有很多的东西,不太明白,对于unity在一帧中调用协成的处理方式,下面是我做的一些测试,在5.3.4版本中进行的测试

using UnityEngine;
using System.Collections;


public class CameraTest : MonoBehaviour {


    bool finish = false;
    bool lateFinish = false;
// Use this for initialization
void Start () {
        //gameObject.GetComponent<Camera>().rect = new Rect(0, 0.1f,0.5f, 0.5f);
        //StartCoroutine(DoSomething());
        Debug.LogError("Start begin");
        StartCoroutine(StartIe());
        Debug.LogError("Start end");
    }

// Update is called once per frame
void Update () {
        if (!finish) {
            Debug.LogError("update begin");
            StartCoroutine(StartIe());
            Debug.LogError("update end");
            finish = true;
        }
        
    }


    void LateUpdate() {
        if (!lateFinish) {
            Debug.LogError("LateUpdate begin");
            StartCoroutine(LateUpdateIe());
            Debug.LogError("LateUpdate end");
            lateFinish = true;
        }
    }


    IEnumerator LateUpdateIe() {
        Debug.LogError("this is  LateUpdate begin");
        yield return new WaitForSeconds(1f);
        Debug.LogError("this is  LateUpdate end");
    }


    IEnumerator UpDateIe() {
        Debug.LogError("this is  Update begin");
        yield return new WaitForSeconds(1f);
        Debug.LogError("this is  Update end");
    }


    IEnumerator StartIe() {
        Debug.LogError("this is start begin");
        yield return new WaitForSeconds(1f);
        Debug.LogError("this is start end");
    }


    //public IEnumerator DoSomething() {
    //    yield return new WaitForSeconds(0.3f);
    //    Debug.LogError("this is falg ");
    //}
}


执行的结果是:


当修改返回值为null的时候:(也就是将上面WaitForSeconds变为null的时候)结果是

可见与上面是一样的,都是在LateUpdate方法的时候进行执行的,在一帧中同时调用多次的该委托,该委托都会从头开始进行执行,在LateUpDate中都会进行执行,猜测unity是有一个保存的链表进行保存了这些委托,在最后进行lateUpdate中进行执行,看网上好多文章都说不建议返回null,说如果返回null,可能在调用中会出现奇怪的问题,这个内容没有遇到过,测试也没有测试出来,此处仅记录一下,以后查错不一定有用

1 0
原创粉丝点击