碰到的問題:
通過GameObject.Instantiate(gameobject)新生成的物體是否執行awake()函數?
猜想:
awake()函數是否只是在程序喚醒時間內執行?
解決方法:
寫一個動態生成物體的小場景測試。
代碼一:
public class TestCube : MonoBehaviour {
public GameObject cube;
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
Instantiate(cube,transform);
}
}
}
代碼二:
public class TestOrder : MonoBehaviour
{
// Use this for initialization
private void Awake()
{
Debug.Log("Awake");
}
private void OnEnable()
{
Debug.Log("OnEnable");
}
void Start()
{
Debug.Log("Start");
}
// Update is called once per frame
void Update()
{
Debug.Log("Update");
}
}
測試結果:
結論:
選了幾個比較有代表性的函數來測試,測試結果是物體被生成後依舊遵循生命週期的函數執行順序。
附上unity生命週期圖: