unity3d常見事件執行順序

測試順序結果:Awake -> OnEnable -> Start -> FixedUpdate -> Update -> LateUpdate -> OnGUI

1.新建一個TestOrder.cs腳本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestOrder: MonoBehaviour
{
    int index = 0;
    void Start()
    {
        index++;
        print("Start: " + index);
    }
    bool Updated = false;
    void Update()
    {
        if (Updated)
            return;
        Updated = true;
        index++;
        print("Update: " + index);
    }
    bool OnEnabled = false;
    private void OnEnable()
    {
        if (OnEnabled)
            return;
        OnEnabled = true;
        index++;
        print("OnEnable: " + index);
    }
    bool Awaked = false;
    private void Awake()
    {
        if (Awaked)
            return;
        Awaked = true;
        index++;
        print("Awake: " + index);
    }
    bool FixedUpdated = false;
    private void FixedUpdate()
    {
        if (FixedUpdated)
            return;
        FixedUpdated = true;
        index++;
        print("FixedUpdate: " + index);
    }
    bool LateUpdated = false;
    private void LateUpdate()
    {
        if (LateUpdated)
            return;
        LateUpdated = true;
        index++;
        print("LateUpdate: " + index);
    }
    bool OnGUIed = false;
    private void OnGUI()
    {
        if (OnGUIed)
            return;
        OnGUIed = true;
        index++;
        print("OnGUI: " + index);
    }
}

2.拖拽到Camera處,運行結果(測試了1000組數據結果沒有變化,執行順序應該是可以排除掉偶然性的)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章