標籤:des style class code tar width
原地址:http://www.cocoachina.com/bbs/read.php?tid=70395&page=1
分享看見的2篇好文.
簡單翻譯了一下並且放出原文
http://www.cratesmith.com/archives/183
1.做減法
大量代碼在Update()或FixedUpdate()中做處理,
意味著無論代碼的執行速度如何,都將在每次幀重新整理的時候調用到,
複製代碼
- public class MyHoming : MonoBehaviour {
-
- public void Update() {
- // expensive targetting and not so expensive trajectory calculations in here
- // currently called every frame
- }
- }
|
但是大多數時候我們並不用這樣做,考慮使用下面的方法
複製代碼
- public class MyHoming : MonoBehaviour {
-
- public void Update() {
- // expensive targetting and not so expensive trajectory calculations in here
- // currently called every frame
- }
- }
|
它看上去更複雜一些,其實不然.只有在必要的時候才會執行代碼.
2.強制指定記憶體回收的頻率
複製代碼
- using UnityEngine;
-
- class GarbageCollectManager : MonoBehaviour {
- public int frameFreq = 30;
- void Update() {
- if (Time.frameCount % frameFreq == 0)
- System.GC.Collect();
- }
- }
|
這個操作不會改進你的fps,但是會降低記憶體的使用量.
3.三角形和繪圖函數的調用計數
保持三角形低於7500
保持Draw Call 低於20000
可能的話使用XCODE中的工具instruments來調試圖形效能.
另外一篇:
http://www.toxicblob.com/files/e61772d99deb1b4926dc5e24f080ce08-6.php
使用Strict
在你所有指令碼的頂部使用 #pragma ,指令碼將是強型別的,避免寫代碼的時候錯誤的類型賦值.
避免Object.Instantiate() 和 Object.Destroy()
Instantiating 和 Destroying 都不好,因為他們需要在建立和銷毀對象的時候動態分配記憶體,這樣會短暫的影響到效能.
考慮的代替方案是使用他寫的SpawnManager類.它在遊戲一開始就初始化所有的對象,這樣能在遊戲載入的時候一同載入對象,從而讓人感覺不到影響.
禁用對象的時候也保持在記憶體中,當需要他們的時候只需要在啟用就行.
還有幾點分別是
Cache Component Lookups
Use iTween Sparingly
Avoid SetActiveRecursively()
Use Builtin Arrays
Avoid String Comparison
Avoid Vector3.magnitude & Vector3.Distance()
可以在引用來源看到詳細說明