1. Awake方法在脚本对象实例化时执行
2. Start是脚本有效时执行
如果在脚本对象实例化后,直接访问脚本对象的属性(初始化后的属性),则属性的初始化需要写在Awake方法
using UnityEngine;
using System.Collections;
public class TestAwakeStart : MonoBehaviour
{
void Start()
{
Create();
}
void Create()
{
//1. Awake在 AddComponent 后执行
//2. Awake执行完后,再执行 AddComponent 的下一行
//3. Start在 Create 后执行
GameObject go = new GameObject("go");
Test test = go.AddComponent<Test>();
Debug.Log("AddComponent Next Line");
}
}
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour
{
void Awake()
{
Debug.Log("Awake");
}
void Start()
{
Debug.Log("Start");
}
}
打印结果:
