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组数据结果没有变化,执行顺序应该是可以排除掉偶然性的)

猜你喜欢

转载自blog.csdn.net/zhunju0089/article/details/103585991