Unity3d Ihpone開發優化

轉自:http://tank2308635.iteye.com/blog/1259218

- 試試把gameobject 一個一個禁用,確定下什麼最影響性能

- stats 裏面有多少個 Draw calls & 三角形

- 合併網格物體

- 合併材質張數

- 不要用網格碰撞.

- Unity對帶動作的模型渲染消耗比較大,如果是靜態的不帶動作文件的模型渲染不會出現效率問題,但是要確保只繪製屏幕內的東西

1. 儘量避免每幀處理

比如:

function Update() { DoSomeThing(); }

可改爲每5幀處理一次:

function Update() { if(Time.frameCount % 5 == 0) { DoSomeThing(); } }

2. 定時重複處理用 InvokeRepeating 函數實現
比如,啓動0.5秒後每隔1秒執行一次 DoSomeThing 函數:

function Start() { InvokeRepeating("DoSomeThing", 0.5, 1.0); }

3. 優化 Update, FixedUpdate, LateUpdate 等每幀處理的函數
函數裏面的變量儘量在頭部聲明。
比如:

function Update() { var pos: Vector3 = transform.position; }

可改爲

private var pos: Vector3; function Update(){ pos = transform.position; }

4. 主動回收垃圾
給某個 GameObject 綁上以下的代碼:

function Update() { if(Time.frameCount % 50 == 0) { System.GC.Collect(); } }

5. 運行時儘量減少 Tris 和 Draw Calls
預覽的時候,可點開 Stats,查看圖形渲染的開銷情況。特別注意 Tris 和 Draw Calls 這兩個參數。
一般來說,要做到:
Tris 保持在 7.5k 以下
Draw Calls 保持在 20 以下

6. 壓縮 Mesh
導入 3D 模型之後,在不影響顯示效果的前提下,最好打開 Mesh Compression。
Off, Low, Medium, High 這幾個選項,可酌情選取。

7. 避免大量使用 Unity 自帶的 Sphere 等內建 Mesh
Unity 內建的 Mesh,多邊形的數量比較大,如果物體不要求特別圓滑,可導入其他的簡單3D模型代替。

8. 優化數學計算
比如,如果可以避免使用浮點型(float),儘量使用整形(int),儘量少用複雜的數學函數比如 Sin 和 Cos 等等。

1、 ArrayList的對象在使用時應該注意以下事項
使用
arraylen = myarray. length;
for (i=0; i< arraylen; i++)
{
}
避免
for (i=0; i<myarray.length; i++) {
}

2、 少用臨時變量
使用
var touch:Vector2;

function Update(){
if (count==0) { touch=iPhone.GeTouch(0).position;}
}
避免
function Update(){
if (count==0) {
var touch:Vector2=iPhone.GeTouch(0).position;
}
}

3、 少用算數符號/,改爲乘以相應小數
使用
x * 0.5f
避免
x/2

4、 動作和AI不需要每幀都執行
if (GLO.count%3){
// execute the enemy aim at your player every 3 Update
}
if (GLO.count%5){
// execute player IA every 5 Update
}

5、 Mesh部分
原文:
- do not use several materials for one mesh. I got the problem with a little model that had 2 materials. It was taking 2 time more polys! When i removed one material, it was showing at half his the size. I don't know why. Usually each material = 1 draw call, so use texture atlas!
個人譯文:
-對於一個mesh不要使用多張materials。我遇到過這樣的問題是一個小模型擁有2張materials導致了2倍甚至更多的時間!而當我減少一個material的時候,時間減少了一半。

原文
- Use polygon cruncher to lower your mesh polys but even if your mesh look a little square, you can compensate this by tweaking "smoothing angle" in the importer inspector. For a highly optimized mesh, change the smoothing angle to 80 or even 120 and you mesh will look more smooth even with just a few polygons.
個人譯文:
使用多邊形粉碎機降低你mesh的面熟,但是雖然你的mesh看起來像一個正方形。你可以通過調整在導入區的” smoothing angle”選項來進行補償。對於一個高優化的mesh,調整平滑角度到80甚至120度,你的mesh將看起來很配合雖然只是一點點的數量(???)

原文
- try not to use any expensive arithmetic like sin() or cos(). instead fill an array with precalculated values with x=0 to 360) sin(x) and cos(x) and use the values of this table. Of course it is in the case of you don't need precision.
個人譯文
不要嘗試使用任何昂貴消耗的數學公式,例如sin() 或 cos(),用預定義好的0到360度的sin(x)和cos(x)的值組成鏈表來替代。當然了前提是你不需要太精確。

原文
- force integer when possible. If you have a counter or check the position on the screen, declare the variable as INT, they are faster.
個人譯文
儘可能的使用整形。如果你有一個檢測屏幕位置的變量或者一個計數器,定義成整形會更快。

Unity3d的一些小竅門
1、防止黑屏
In script change iPhoneSettings.screenCanDarken to false. This basically does the same as:
//Objective-C call [UIApplication sharedApplication].idleTimerDisabled = YES;
It prevents the auto lock on iPhone to kick-in.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章