優化技巧
於Update ()之中大量使用需要搜尋元件或物件的函數,如:GetComponent
()或GameObject.Find("ObjectName")等等皆須耗費大量效能,盡量在Start ()或Awake ()就完成這些動作,如果非要每個Frame搜尋,建議使用GameObject.FindWithTag。 多使用Collider、Trigger或Event delegate來觸發或通知狀態的改變。
對Mesh物件使用Raycasting,可利用Camera的Culling Mask濾掉不存在畫面裡的物件。
int layerMask = 1 << 8; // The index of player layer is 8.
// Does the ray intersect any objects which are in the player layer.
if (Physics.Raycast(transform.position, Vector3.forward, Mathf.Infinity, layerMask))
Debug.Log("The ray hit the player");
使用Bake光照貼圖取代動態光照。
將Mesh Compress壓縮比調製可接受的最大值。
定期做Garbage Collection。
if( Time.frameCount % m_frameFreq == 0 )System.GC.Collect();