Unity3D腳本間執行順序
Unity3D中一個場景有時候需要多個腳本,可以掛在同一物體上執行,也可以掛在不同物體上執行
那麼執行順序是怎樣的?我們來測試下
在上個項目基礎上,再建一個Test2腳本如下
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Test2 : MonoBehaviour { // Start is called before the first frame update void Start() { Debug.Log('2'); } // Update is called once per frame void Update() { } }
把Test2腳本也拖拽到Test物體上。可以看到腳本的掛載順序是Test、Test2
然後我們運行一下,腳本是按掛載順序逆序執行的。然後測試掛載不同物體上也是一樣的結果
總結:腳本執行順序默認是按掛載順序逆序執行
自定義腳本執行順序
那我想要自定義腳本執行順序該怎麼辦呢?
隨便打開一個腳本->Execution Order點擊添加test、test2,點擊Apply(數值越小越先執行)
英文版本的路徑:Inspector->Execution Order->MonoManager 設置執行順序(數值越小越先執行)->Apply