unity5单个gameobject各函数的执行顺序,跟unity3不同:
Awake(Awake是先于OnEnable的,不管是否启用,都会执行)
OnEnable
Start
FixedUpdate
WaitForFixedUpdate
Update
LateUpdate
YieldNull
OnWillRenderObject
OnGUI
WaitForEndOfFrame
WaitForSeconds
OnDisable
同一个GameObject上的多个脚本是按类名顺序执行的:
AScript.Start
BScript.Start
AScript.Update
BScript.Update
......
父子关系的多个gameobject各函数的执行顺序:
兄弟关系的也一样,总的思想是显示在上层的gameobject的脚本先执行,显示在底层的gameobject后执行,体现了叶子依赖根的思想。
all.AwakeAndOnEnable(){
child.Awake
child.OnEnable
parent.Awake
parent.OnEnable
}
all.Start(){
//以后的都是这样
child.Start
parent.Start
}
……
all.OnWillRenderObject(){
//这里是个特例,肯定是父亲先显示,子才能显示
parent.OnWillRenderObject
child.OnWillRenderObject
}
之所以设计出这样是因为一般Manager脚本挂在父亲上,在父亲的start里面可以设置子女的参数,子代的脚本一般不要依赖父亲的脚本了,如果这么做的话,就要在子代脚本里面做延迟处理了,不推荐。
注意:可以在Inspector中手动调节脚本执行顺序。
using UnityEngine;
using System.Collections;
public class OrderTest : MonoBehaviour {
public string name = string.Empty;
void Awake ()
{
Debug.Log(name+":Awake");
}
void OnEnable () {
Debug.Log (name+":OnEnable");
}
void Start () {
Debug.Log (name+":Start");
StartCoroutine (YieldNull());
StartCoroutine (FWaitForSeconds());
StartCoroutine (FWaitForFixedUpdate ());
StartCoroutine (FWaitForEndOfFrame ());
}
void FixedUpdate(){
Debug.Log (name+":FixedUpdate");
}
IEnumerator FWaitForFixedUpdate(){
yield return new WaitForFixedUpdate ();
Debug.Log (name+":WaitForFixedUpdate");
}
void Update () {
Debug.Log (name+":Update");
}
void LateUpdate(){
Debug.Log (name+":LateUpdate");
}
IEnumerator YieldNull(){
yield return null;
Debug.Log (name+":YieldNull");
}
void OnWillRenderObject(){
Debug.Log (name+":OnWillRenderObject");
}
void OnGUI(){
Debug.Log (name+":OnGUI");
}
IEnumerator FWaitForEndOfFrame(){
yield return new WaitForEndOfFrame ();
Debug.Log (name+":WaitForEndOfFrame");
}
IEnumerator FWaitForSeconds(){
yield return new WaitForSeconds(1);
Debug.Log (name+":WaitForSeconds");
}
void OnDisable(){
Debug.Log (name+":OnDisable");
}
}
using System.Collections;
public class OrderTest : MonoBehaviour {
public string name = string.Empty;
void Awake ()
{
Debug.Log(name+":Awake");
}
void OnEnable () {
Debug.Log (name+":OnEnable");
}
void Start () {
Debug.Log (name+":Start");
StartCoroutine (YieldNull());
StartCoroutine (FWaitForSeconds());
StartCoroutine (FWaitForFixedUpdate ());
StartCoroutine (FWaitForEndOfFrame ());
}
void FixedUpdate(){
Debug.Log (name+":FixedUpdate");
}
IEnumerator FWaitForFixedUpdate(){
yield return new WaitForFixedUpdate ();
Debug.Log (name+":WaitForFixedUpdate");
}
void Update () {
Debug.Log (name+":Update");
}
void LateUpdate(){
Debug.Log (name+":LateUpdate");
}
IEnumerator YieldNull(){
yield return null;
Debug.Log (name+":YieldNull");
}
void OnWillRenderObject(){
Debug.Log (name+":OnWillRenderObject");
}
void OnGUI(){
Debug.Log (name+":OnGUI");
}
IEnumerator FWaitForEndOfFrame(){
yield return new WaitForEndOfFrame ();
Debug.Log (name+":WaitForEndOfFrame");
}
IEnumerator FWaitForSeconds(){
yield return new WaitForSeconds(1);
Debug.Log (name+":WaitForSeconds");
}
void OnDisable(){
Debug.Log (name+":OnDisable");
}
}