关于新生成的物体生命周期执行顺序问题

来源:互联网 发布:西安行知中学杨凌分校 编辑:程序博客网 时间:2024/06/05 09:17

碰到的问题:

通过GameObject.Instantiate(gameobject)新生成的物体是否执行awake()函数?

猜想:

awake()函数是否只是在程序唤醒时间内执行?

解决方法:

写一个动态生成物体的小场景测试。

代码一:

public class TestCube : MonoBehaviour {    public GameObject cube;    private void Update()    {        if (Input.GetMouseButtonDown(0))        {            Instantiate(cube,transform);        }    }}
代码二:

public class TestOrder : MonoBehaviour{    // Use this for initialization    private void Awake()    {        Debug.Log("Awake");    }    private void OnEnable()    {        Debug.Log("OnEnable");    }    void Start()    {        Debug.Log("Start");    }    // Update is called once per frame    void Update()    {        Debug.Log("Update");    }}
测试结果:


结论:

选了几个比较有代表性的函数来测试,测试结果是物体被生成后依旧遵循生命周期的函数执行顺序。


附上unity生命周期图:


原创粉丝点击