前幾天去北京參加了unite2014大會,舉辦的還不錯。老外來的都是引擎的開發程序員,所以他們講的內容基本都是unity5的一些新技術,講解過程有同聲傳譯,不過有些技術詞彙不翻譯還聽得懂一點(今年得練英文口語和聽力了,希望下次能直接和老外交流)。國內的技術專家講的內容更貼近開發一些,一些優化相關的內容還是非常有幫助的。
開始以爲參會的可以下載講師的ppt,所以沒有進行太多的記錄,只有些零散的筆記,而且我一個人也只能聽完一半的技術講座。下面就是稍微整理了一下的筆記:
Unity開發四大準則
1. 美術資源量對於程序發佈包的大小、性能優化、內存佔用量的影響,往往超過其他種種因素。
技術美術和關卡設計師對於遊戲性能承擔着重要的責任;
程序員往往無法補救由於濫用美術資源而造成的性能問題;
2. 項目團隊應該通過編寫工具來保證美術資源的合法性。
美術規範文檔無法再實際上保證美術資源的合法性;
程序員應該通過Unity編輯器擴展技術,爲美術師實現完整的美術資源合法性檢查工具;
3. 對渲染效率和內存佔用的優化應該在項目實施過程中反覆進行。
針對CPU/遊戲邏輯的的優化往往能夠比針對GPU/Shader的優化取得更大的做用;
GPU/Shader的性能優化應該放在最後進行;
善用UnityProfilers;
4. 從反向工程的角度理解項目開發,以最終需要達到的目的來決定程序的架構設計以及關鍵技術的方案選擇。
是否需要進行代碼的增量更新;
是否需要嚴格控制程序發佈包的大小;
是否需要支持低端移動設備等等;
手遊資源優化
貼圖儘量合併;
使用substance texture減少資源量;
字體顏色和一些效果可以用shader來實現;
儘量減少mesh數據;
可能的話使用lod;
儘可能利用dynamic batching和staticbatching減少draw call;
控制骨骼數量和骨骼影響的頂點數;
所有的角色動畫放在一個文件中;
優化動畫關鍵幀;
音頻,視頻控制質量;
避免實時計算的東西;
Texture
Use GPU friendly texture(ETC1, ETC2, ASTC)
Build for specific GPU(DTX, PVRTC, ATC)
Build Atlas when possible(with/withtoualpha)
Use Sustance procedural texture
Use Unity Dynamic Texture
Runtime Creation/ Altas Packing /Compression
Font
Use TTF instead of Texture
Remove unused character from TTF files
Use Native Font(Android 2.3/4.1)
Add Shader Effect(Colorize/Outline)
Shader
Use build-in Unity 'mobile' shader
Build full custom shader per GPU
Reduce number of instruction
Reduce the number of texture fetch
ToAvoid .. Expensive
function: pow, exp, log, cos, sin, tan
custom operation: normalize, dot,inverseqrt
'Discard' fixed function(Alpha Test)
'ColorMask' render setup that turn off channel render
Reflection
Fog
Float Precision
Use smallest float precision when possible
float/highp : 32-bit(vertex shader, for vertex transformation)
half/mediump :16-bit(texture UV coordinates)
fixed/lowp :10-bit(color, unit length vector, light calculation)
DynamicBatching
Dynamic Batching(on moving object)
Apply automaticly
withLess than 900 vertex(VBO)
with Same Material
with Same Uniform Scale/Different Non-Uniform Scale
without Multi-Pass Shaders
without Receiving real-time shadows
StaticBastching
Static Batching(on non-moving object)
Require additional memory
with 'Mesh Renderer' or 'Particle Renderer'(not GUI)
Canbe mark on non-moving mesh part that have animation
Combine static mesh inside the samemesh(sub-mesh)
Skinning
Reduce number of bone in skinned mesh
Only keep bone in hierachy that will affectthe skinning
Reduce number of bone influence per vertex
Aniamtion
Merge animation using the same hierachy in one FBX
Use same animation on multiple skinned object
Bled animation(Mecanim Animation System)
KeyReduction
Post-Process in Unity or Pre-Process In FBX
Audio
Force to Mono/Decrease kbps
Uncompress WAV & Compress MP3 case
Uncompress (shortClip/SoundFX)
Compressed in Memory (Longer/Looping clip)
Stream form disc (Music)
Decompress on load (Clipcausing CPU process spike)
Require additional memory
Video
Create Cut-Scene instead of Cinematic
Convert into Theora OGG format
Reduce kbps(use different quality for Video/Audio)
Light
Avoid Real-Time lighting
use lightmap..(increase APK size & decrease Performance)
if needed, use a maximum of 1 directional light
Avoid Real-Time shadow
use texture shadow instead
性能問題
對Profiler的分析需要貫穿於整個項目開發期。
弄清每一個參數的含義;
如果GC相關函數時間長,會造成“頓卡”,查看內存分配,注意每幀會產生的堆內存分配;
AssetBundle要進行依賴打包,否則會造成包和內存都增大;
目前官方自帶的GUI佔用內存大、效率低;
Poolmanager是很有用的內存和對象管理插件;
(華爲某芯片對shader解析非常慢)
優化局部代碼:
使用Profiler.BeginSample()和Profiler.EndSample();
C#一些高級功能會造成額外的內存分配(foreach、LINQ、Lambda表達式等);
String連接使用StringBuilder;
Get相關函數會造成查找,儘量避免在Update內直接調用;
一些插件也有可能造成內存分配問題;
將代碼包含在下載包內
參看官方文檔Included scripts in AssetBundles。
代碼增量更新
原理:Unity腳本經過編譯後就不能再修改,但是可以添加
方案2:將接口與實現分離
不改變用戶工作流程
仍可在編輯器中使用動態編輯
對代碼起到一定的保護作用(可以混淆)
對原有的AssetBundles打包方案沒有任何影響
(代碼打包不適合iOS,因爲不支持反射)
ResourcesVS Assetbundle
Resources 加載快,內存佔用少,無法更新,文件夾裏有2G的限制
Assetbundle動態加載,內存佔用大
如何使用
一些不變的資源用Resources
變的用Assetbundle
開發版本用Resources,發佈版本用Assetbundle
創建AssetBundle的選項
CollectDependencies 包含所有的依賴的Assets
CompleteAssets 包含Object所屬的Asset的所有Objects
DisableWriteTypeTree 不寫入TypeTree
DeterministicAssetBundle每個Object有唯一的內部ID,適用於增量式打包AssetBundle
UncompressedAssetBundle不進行數據壓縮
依賴關係打包
比如有2個包中都含有mat,則需要將mat單獨打成1個包,參看官方文檔Managing asset dependencies相關內容。
如何獲取資源依賴關係
Assetdatabase.GetDependencies
分析資源依賴關係
統計輸出沒必要的資源
AssetBundle發佈方式
1. AssetBundle進安裝包
壓縮格式
把AssetBundle放到SteamingAssets目錄下
WWW加載
2. AssetBundle不進安裝包(單獨下載)
非壓縮格式或壓縮格式
製作一整個壓縮文件(7z或zip)
下載解壓
CreateFromFile加載或WWW
加載
Build輸出的依賴關係文件(xml,json等)
建議一個隊列,Sheduler類進行調度加載
被依賴的assetbundle要等到上層assetbundle加載完纔可以unloadassetbundle(false)
釋放
在切換場景時,釋放GameObjet,資源引用置爲null,釋放assetbundle。
AssetBundle.Unload(true)
調用Resources.UnloadUnusedAsests
異步
一些有用的插件:
加載優化Poolmanager5,優化資源加載和管理,優化gc
測試插件Unity Test Tool(本次有作爲單獨講座講解,可惜我沒有參加)
遊戲分享Everyplay
動畫finalIk
運行時效率統計FpsGraph
雜項:
Unity5會做新的AssetBundle系統;
會增加UNet(這個我只聽了一點,不知道架構和photon引擎是不是類似的,似乎也是有cloud服務器的)