新建的C#脚本
Unity中新建一个C#脚本,会内置2个方法,Start()和Update(),与Start()的相似的还有个Awake()方法
但是Start()和Awake()有什么区别呢
执行顺序
当一个附带Script的GameObject被载入时,执行顺序如下
Ref:官方文档
- Awake()
References between scripts, initialistation
- Start()
Once script component is enabled
- Update()
可以看出,Awake()和Start()都是载入后被立即执行,且Awake()优先于Start()
区别
但是当Inspector中把GameObject的Script取消勾选时,Awake()依然会执行,但是Start()就不会执行了
官方的建议
For example an enemy character could enter the game and use Awake() to have ammo count assigned to him, but only get the ability to shoot, using Start() at a defined time when that script component is enabled. It should be noted however that Start() and Awake() will only ever be called once in the lifetime of a script attached to an object.
我的理解就是,Awake()负责初始化,Start()负责改变状态