Unity3d iOS基本最佳化和進階最佳化

來源:互聯網
上載者:User

標籤: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()中做處理,
意味著無論代碼的執行速度如何,都將在每次幀重新整理的時候調用到,

 複製代碼
  1. public class MyHoming : MonoBehaviour {  
  2.   
  3.     public void Update() {  
  4.         // expensive targetting and not so expensive trajectory calculations in here  
  5.         // currently called every frame  
  6.     }  
  7. }



但是大多數時候我們並不用這樣做,考慮使用下面的方法

 複製代碼
  1. public class MyHoming : MonoBehaviour {  
  2.   
  3.     public void Update() {  
  4.         // expensive targetting and not so expensive trajectory calculations in here  
  5.         // currently called every frame  
  6.     }  
  7. }  



它看上去更複雜一些,其實不然.只有在必要的時候才會執行代碼.



2.強制指定記憶體回收的頻率

 複製代碼
  1. using UnityEngine;  
  2.   
  3. class GarbageCollectManager : MonoBehaviour {  
  4.     public int frameFreq = 30;  
  5.     void Update()   {  
  6.         if (Time.frameCount % frameFreq == 0)  
  7.             System.GC.Collect();  
  8.     }  
  9. }  



這個操作不會改進你的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()

可以在引用來源看到詳細說明 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.