本篇簡單介紹Unity3d中幾種Update方法的區別。
Update方法
所有
Update
Update是在每次渲染新的一幀的時候才會調用,也就是說,這個函數的更新頻率和設備的性能有關以及被渲染的物體(可以認為是三角形的數量)。在性能好的機器上可能fps 30,差的可能小些。這會導致同一個游戲在不同的機器上效果不一致,有的快有的慢。因為Update的執行間隔不一樣了。
FixedUpdate
FixedUpdate是在固定的時間間隔執行,不受游戲幀率的影響。Tips:在處理Rigidbody的時候最好用FixedUpdate。
FixedUpdate設置:Edit --> Project Settings --> Time

LateUpdate
LateUpdate是在所有Update函數調用后被調用。這可用於調整腳本執行順序。
Update和FixedUpdate的區別
- FPS = 2;
using UnityEngine;
using System.Collections;
public class FPS : MonoBehaviour {
void Awake() {
Application.targetFrameRate = 2;
}
void Update () {
Debug.Log ("Update");
}
void FixedUpdate () {
Debug.Log ("FixedUpdate");
}
}

- FPS = 60;
using UnityEngine;
using System.Collections;
public class FPS : MonoBehaviour {
void Awake() {
Application.targetFrameRate = 60;
}
void Update () {
Debug.Log ("Update");
}
void FixedUpdate () {
Debug.Log ("FixedUpdate");
}
}

