假设prefab挂载了一个A MonoBehavior,有一个public void Test() 方法。使用GameObject.Instantiate这个prefab,
A a = instPanleGO.GetComponent<A>(); a.Test(); 为什么执行顺序是Awake->Test->Start?有点不明白。
老师您好!可能我的问题没有描述清楚。我把代码和日志贴一下吧。
public void CreateGameObjgect() { string prefabPath = "UIPanel/SystemPanel"; GameObject go = GameObject.Instantiate(Resources.Load(prefabPath)) as GameObject; go.transform.SetParent(parentTransform, false); //这里添加一个组件,会触发声明周期Awake。能理解Awake优先于其它方法调用 TestComponet testComponet = go.AddComponent<TestComponet>(); //为什么Test会优先于Start调用 testComponet.Test(); } public class TestComponet : MonoBehaviour { private void Awake() { print("TestComponet#Awake"); } private void Start() { print("TestComponet#Start"); } public void Test() { print("TestComponet#Test"); } }
// 打印日志
TestComponet#Awake--》TestComponet#Test--》TestComponet#Start