昨天被问到一个问题MonoBehaviour的Awake()和Start()的区别是啥?
当然Awake()会在Start()之前被调用,这个地球人都知道了。如果只是顺序问题,那当然就没太大必要搞两个函数了。仔细研究了一下API文档:
using UnityEngine; using System.Collections; public class TryObject : MonoBehaviour { // Use this for initialization void Start() { #if true GameObject dynaGO = new GameObject("DynamicGO"); dynaGO.AddComponent<DynamicObject>(); #else Object prefab = Resources.Load("DynamicGO"); Object instance = GameObject.Instantiate(prefab); #endif } }另外一个脚本就写几个空函数,用来打断点:
using UnityEngine; using System.Collections; public class DynamicObject : MonoBehaviour { void Awake() { } // Use this for initialization void Start() { } // Update is called once per frame void Update() { } }以下是使用AddComponent()方法时,DynamicObject:Awake()的调用堆栈:
Unity3D脚本中的Awake()和Start()的本质区别
原文地址:http://blog.csdn.net/neil3d/article/details/43865575