Tag Archives: Unity
[Unity]Update,FixedUpdate,LastUpdate的区别
官方文档
Update Order
When you’re keeping track of game logic and interactions, animations, camera positions, etc., there are a few different events you can use. The common pattern is to perform most tasks inside the Update function, but there are also other functions you can use.
FixedUpdate
FixedUpdate is often called more frequently than Update. It can be called multiple times per frame, if the frame rate is low and it may not be called between frames at all if the frame rate is high. All physics calculations and updates occur immediately after FixedUpdate. When applying movement calculations inside FixedUpdate, you do not need to multiply your values by Time.deltaTime. This is because FixedUpdate is called on a reliable timer, independent of the frame rate.
Update
Update is called once per frame. It is the main workhorse function for frame updates.
LateUpdate
LateUpdate is called once per frame, after Update has finished. Any calculations that are performed in Update will have completed when LateUpdate begins. A common use for LateUpdate would be a following third-person camera. If you make your character move and turn inside Update, you can perform all camera movement and rotation calculations in LateUpdate. This will ensure that the character has moved completely before the camera tracks its position.
自己的理解
Update()是实机帧调用,受GameObject的渲染影响,每次的Time.deltaTime都是不一样的
而FixedUpdate()则是固定间隔,风雨无阻,游戏多卡FixedUpdate()也会按时调用
LastUpdate()是紧跟Update(),给Update()擦屁股的函数
所以官方建议在FixedUpdate做一些物理引擎相关的事
[Unity]Awake和Start的区别
新建的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()负责改变状态