優(yōu)化公司流程制度百度seo關(guān)鍵詞排名 s
序
有的時(shí)候,執(zhí)行某些腳本時(shí)會(huì)有先后順序的要求。unity是按什么順序來執(zhí)行腳本的?如何設(shè)置?
默認(rèn)的執(zhí)行順序
官方文檔里面有個(gè)很長(zhǎng)的圖:
Unity - Manual: Order of execution for event functions (unity3d.com)
?
根據(jù)文檔,在單個(gè)腳本里,函數(shù)的執(zhí)行順序是是Awake、Start、Update……
如果用到了多個(gè)腳本,就會(huì)存在多個(gè)Awake,多個(gè)Start,這時(shí)候應(yīng)該按什么順序來?
根據(jù)這個(gè)網(wǎng)頁[1]:
先把所有腳本的Awake執(zhí)行完,然后再執(zhí)行所有腳本的Start……
那么,執(zhí)行所有腳本start的時(shí)候,哪個(gè)腳本的start先執(zhí)行?哪個(gè)腳本的start后執(zhí)行?
根據(jù)這個(gè)網(wǎng)頁[2]?:
誰的start先執(zhí)行,誰的start后執(zhí)行,默認(rèn)是有一個(gè)順序的。
自定義執(zhí)行順序
但是,有的時(shí)候,默認(rèn)的順序不符合要求,需要自定義,這個(gè)怎么做?
比如,存在scriptA和scriptB,內(nèi)容如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class scriptA : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){print("this is scriptA");}// Update is called once per framevoid Update(){}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class scriptB : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){print("this is script B");}// Update is called once per framevoid Update(){}
}
按默認(rèn)順序的話,是先B后A:
但是實(shí)際使用的時(shí)候,需要先A后B,怎么辦?如何設(shè)置?
打開窗口,點(diǎn)擊加號(hào),添加scriptA和scriptB,讓scriptA排在scriptB的前面,就可以了。執(zhí)行的時(shí)候,就會(huì)先執(zhí)行A,再執(zhí)行B了。
再執(zhí)行,符合預(yù)期:
引用鏈接
[1]????????Unity不同腳本之間的執(zhí)行順序-騰訊游戲?qū)W堂 (tencent.com)?
[2]????????設(shè)置unity腳本的執(zhí)行順序-CSDN博客
相關(guān)鏈接
【Unity3D日常開發(fā)】Unity3D中實(shí)現(xiàn)不同腳本之間的執(zhí)行順序控制_unity update執(zhí)行順序-CSDN博客
unity如何手動(dòng)更改腳本執(zhí)行順序_untiy 設(shè)置腳本執(zhí)行順序-CSDN博客?