在使用Unity3D工程導出安卓安裝包的時候,每每會遇到兼容性的問題,針對某些機型,要麼沒法打開遊戲,要麼會出現卡機的現象。面對這種狀況,咱們能夠調節相關的參數來提升兼容性。
javascript
爲了瞭解在打包時候每一個參數的含義,咱們查找了Unity3D的官方文檔。爲了清楚每一個參數所起的做用以及每一個參數對兼容性的影響因素,咱們有針對性的作了對比實驗,而後在安卓雲測試平臺(http://www.testin.cn)測試兼容性,將最終結果進行比較,最後提出優化方案。
java
Unity4.5安卓打包參數配置主要講述Unity4.5在進行安卓打包時遇到的一些參數,這些參數的詳細解釋,以及如何經過修改參數來提升遊戲的兼容性。本文主要描述如何配置打包參數,供Unity4.5輸出安卓包的工做人員使用。
android
本文檔面向瞭解Android,Unity3D客戶端的開發人員。
多線程
一.Rendering Path(This property is sharedbetween Standalone and WebPlayer content)
app
渲染路徑,這個屬性通常在PC上或者網頁上比較有用,通常有以下三個選項
ide
Vertex Lit(Lowest lighting fidelity, no shader support.Best used on old machines or limited mobile platforms)
測試
頂點光源模型。不支持shader,在一些舊的機器上或者受限制的機器上最好使用這個選項
優化
Forward(Good support for lighting features; limitedsupport for shadows)
網站
對光的特徵支持的很好,可是對陰影支持的很差
ui
Deferred Lighting(Best support for lighting and shadowingfeatures, but requires certain level of hardware support. Best used if you havemany realtime lights. Unity Pro only)
對光的特徵和陰影支持的很好,前提是支持硬件加速,若是作實時渲染最好使用這個選項,只有pro版本支持。
二.Graphics Level(Select either ES 1.1 (‘fixed function’) or ES 2.0 (‘shader based’) Open GL level. Whenusing the AVD (emulator) only ES 1.x is supported.)
圖像等級。在OpenGLES 2.0(支持陰影)和3.0還有1.x(功能性)之間選擇,當只用到模擬器的時候,只有ES1.x才支持。
Automatic
自動選擇
Force OpenGLES 2.0
強制OpenGL ES2.0
Force OpenGLES 3.0
強制OpenGL ES3.0
三.Install Location
安裝路徑。
PreferExternal
優先外部
Automatic
自動選擇(通常都是自動選擇,並且兼容性更好)
ForceInternal
強制安裝在內部閃存中
四.Write Access
寫入權限
InternalOnly
只能寫在內部
External(SDCard)
寫在外部SD卡中,對應安卓的寫入權限:android.permission.WRITE_EXTERNAL_STORAGE
五.Api Compatibility Level
使用.Net的版本
.Net 2.0Subset
.Net2.0的子集,佔用較小的空間
.Net 2.0
.Net2.0徹底集合,佔用較大的空間
六.Stripping Level
選擇抽出的腳本功能來減小了包的大小(iOS和Android均可以選擇)
Disabled
不減小功能
StripAssemblies
減小程序集合
StripByteCode
減小字節碼
Use micromscorlib
使用迷你庫,前二者都減小
七.Optimize Mesh Data*
對於材料沒有必要的數據,能夠移除的數據進行優化
八,Multithreaded Rendering
多線程渲染
九.GPU Skinning*
是否使用GPU着色
十.Static Batching
是否激活靜態度量,默認是激活狀態,只有pro版本有效。
十一.Dynamic Batching
是否激活動態度量,默認是激活轉檯。
該實驗採起對照實驗的方法。若是要考察單個參數對於兼容性的影響,必須得控制另外的參數不變。而後針對不一樣的配置參數打包,上傳到雲測試平臺進行兼容性測試。
對要進行測試的參數進行設計。
主要涉及到以下幾個參數
1. Graphics Level(GL)
2. Install Location(IL)
3. Api Compatibility Level(ACL)
4. Stripping Level(SL)
5. Optimize Mesh Data*(OMD)
6. Multithreaded Rendering*(MR)
7. GPU Skinning*(GK)
8. Write Access(WA)
9. Rendering Path(RP)
10. Static Batching(SB)
11. Dynamic Batching(DB)
設計對照組方案
序號 | GL | IL | ACL | SL | OMD | MR | GK | WA | RP | SB | DB |
1 | 2.0 | external | 2.0subset | disable | false | false | false | Internal only | Forward | true | true |
2 | auto | external | 2.0subset | disable | false | false | false | Internal only | Forward | true | true |
3 | 3.0 | external | 2.0subset | disable | false | false | false | Internal only | Forward | true | true |
4 | auto | auto | 2.0subset | disable | false | false | false | Internal only | Forward | true | true |
5 | auto | internal | 2.0subset | disable | false | false | false | Internal only | Forward | true | true |
6 | auto | internal | 2.0 | disable | false | false | false | Internal only | Forward | true | true |
7 | auto | internal | 2.0subset | Assemblies | false | false | false | Internal only | Forward | true | true |
8 | auto | internal | 2.0subset | ByteCode | false | false | false | Internal only | Forward | true | true |
9 | auto | internal | 2.0subset | mscorlib | false | false | false | Internal only | Forward | true | true |
10 | auto | internal | 2.0subset | disable | true | false | false | Internal only | Forward | true | true |
11 | auto | internal | 2.0subset | disable | true | true | false | Internal only | Forward | true | true |
12 | auto | internal | 2.0subset | disable | true | true | true | Internal only | Forward | true | true |
13 | auto | internal | 2.0subset | disable | true | true | true | SDCard | Forward | true | true |
14 | auto | internal | 2.0subset | disable | true | true | true | Internal only | VertexLit | true | true |
15 | auto | internal | 2.0subset | disable | true | true | true | Internal only | Defferred Lig | true | true |
16 | auto | internal | 2.0subset | disable | true | true | true | Internal only | Forward | false | true |
17 | auto | internal | 2.0subset | disable | true | true | true | Internal only | Forward | true | false |
針對每個設計方案打包。
將上述的17個方案進行打包分別命名爲001-017.apk
將打好的安裝包進行兼容性測試。
將這17個安裝包上傳給testin網站,進行兼容性測試
獲得測試結果後,得出每一個參數的最優化配置。
正常 |
啓動(s) | CPU(%) | 內存(M) | 包體 | |||||||
序號 | 經過率 | 安裝 | 啓動 | 卸載 | 平均值 | 峯值 | 平均值 | 峯值 | 平均值 | 峯值 | |
001 | 74.30 | 87.30 | 85.1 | 100 | 0.91 | 11.67 | 9.70 | 100 | 19.52 | 66.75 | 8205 |
002 | 66.34 | 82.80 | 80.1 | 100 | 0.95 | 13.09 | 9.47 | 100 | 17.82 | 66.87 | 8205 |
003 | 67.44 | 82.70 | 81.5 | 100 | 0.94 | 11.14 | 9.35 | 100 | 17.46 | 65.63 | 8205 |
004 | 70.21 | 83.00 | 84.6 | 100 | 1.03 | 12.42 | 8.81 | 100 | 17.33 | 65.81 | 8205 |
005 | 69.86 | 83.30 | 83.8 | 100 | 1.02 | 10.81 | 8.82 | 100 | 17.61 | 66.88 | 8205 |
006 | 73.96 | 87.20 | 84.9 | 100 | 1.10 | 12.18 | 9.00 | 100 | 19.09 | 66.01 | 8972 |
007 | 73.81 | 87.10 | 84.8 | 100 | 1.02 | 11.12 | 9.01 | 100 | 18.25 | 65.19 | 7243 |
008 | 80.20 | 90.80 | 88.3 | 100 | 1.14 | 11.27 | 9.13 | 100 | 19.85 | 60.29 | 7243 |
009 | 82.43 | 90.90 | 90.7 | 100 | 1.13 | 11.50 | 9.13 | 100 | 19.79 | 60.46 | 7243 |
010 | 79.68 | 90.20 | 88.4 | 100 | 1.04 | 10.93 | 9.40 | 100 | 21.29 | 68.79 | 8205 |
011 | 78.93 | 89.00 | 88.7 | 100 | 1.01 | 10.84 | 9.96 | 100 | 23.41 | 77.06 | 8205 |
012 | 78.55 | 89.00 | 88.3 | 100 | 1.00 | 11.24 | 11.24 | 100 | 23.68 | 77.04 | 8205 |
013 | 87.79 | 97.30 | 90.2 | 100 | 0.92 | 10.76 | 11.80 | 100 | 25.76 | 72.27 | 8205 |
014 | 88.76 | 98.00 | 90.6 | 100 | 1.02 | 11.11 | 10.90 | 100 | 25.36 | 75.57 | 8205 |
015 | 92.70 | 99.20 | 94.5 | 100 | 1.23 | 11.20 | 10.84 | 100 | 26.87 | 71.95 | 8205 |
016 | 93.17 | 98.00 | 95.1 | 100 | 1.15 | 11.72 | 11.61 | 100 | 26.34 | 72.01 | 8205 |
017 | 88.00 | 97.60 | 90.2 | 100 | 1.11 | 10.90 | 11.17 | 100 | 25.87 | 71.96 | 8205 |
對照組信息表
序號 | 參數 | 對照組 | 優化順序 |
1 | Graphics Level | 001:002:003 | ES2.0 > ES3.0 > Automatic |
2 | Install Location | 002:004:005 | Automatic > Force Internal > Prefer External |
3 | Api Compatibility Level | 005:006 | .Net2.0 > .Net2.0 Subset |
4 | Stripping Level | 005:007:008:009 | Use micro mscorlib > Strip ByteCode > Strip Assemblies > Disable |
5 | Optimize Mesh Data* | 005:010 | True > false |
6 | Multithreaded Rendering* | 010:011 | False > true |
7 | GPU Skinning* | 011:012 | False > true |
8 | Write Access | 012:013 | SDCard > Internal Only |
9 | Rendering Path | 012:014:015 | Deferred Lighting > VertexLit > Forward |
10 | Static Batching | 012:016 | False > true |
11 | Dynamic Batching | 012:017 | False > true |
上表就是實驗的最終結果,能夠用來指導Unity3D的安卓打包。
在unity打包的時候還有一個系統最低版本的適配問題,好比支持最低在安卓系統2.3.3上運行。
爲了考察支持最低安卓系統版本對於兼容性的影響。我也作了系統的對比實驗。
序號 | 最低版本 | 目標版本 |
1 | 10 | 14 |
2 | 11 | 14 |
3 | 12 | 14 |
4 | 14 | 14 |
如下是測試結果:
正常 |
啓動(s) | CPU(%) | 內存(M) | 包體 | |||||||
序號 | 經過率 | 安裝 | 啓動 | 卸載 | 平均值 | 峯值 | 平均值 | 峯值 | 平均值 | 峯值 | |
1 | 88.10 | 95.8 | 91.9 | 100 | 0.94 | 10.29 | 14.95 | 100 | 22.37 | 66.10 | 7363 |
2 | 89.74 | 91.0 | 98.6 | 100 | 1.09 | 4.32 | 11.08 | 100 | 25.81 | 65.69 | 7363 |
3 | 90.24 | 92.1 | 98.0 | 100 | 1.28 | 11.30 | 12.75 | 100 | 23.88 | 65.75 | 7363 |
4 | 90.12 | 92.0 | 98.0 | 100 | 1.23 | 10.72 | 12.41 | 100 | 23.84 | 66.07 | 7363 |
因爲如今的主流安卓系統的版本都比較高(都大於4.0 API level = 14),因此咱們將目標版本設置爲14。至於最低的版本,從數據上來看2.3.3(API level = 10)和4.0.0(API level = 14)差異不是很大。因此最低系統版本的選擇對於兼容性的效果提高也不是那麼明顯。介於歷史殘留問題,如今使用2.3.3版本的人數還不少,故能夠將系統最低版本設置爲10。
爲了考察支持目標版(targetsdk level)本對於兼容性的影響,我也作了系統的對比實驗。
序號 | 最低版本 | 目標版本 |
1 | 10 | 10 |
2 | 10 | 11 |
3 | 10 | 12 |
4 | 10 | 13 |
5 | 10 | 14 |
一下是測試結果:
正常 |
啓動(s) | CPU(%) | 內存(M) | 包體 | |||||||
序號 | 經過率 | 安裝 | 啓動 | 卸載 | 平均值 | 峯值 | 平均值 | 峯值 | 平均值 | 峯值 | |
1 | 85.62 | 95.8 | 87.4 | 100 | 0.90 | 10.62 | 14.70 | 100 | 21.60 | 65.60 | 7363 |
2 | 80.37 | 91.0 | 81.9 | 100 | 0.86 | 10.44 | 12.40 | 100 | 21.62 | 75.63 | 7363 |
3 | 81.93 | 92.1 | 84.0 | 100 | 0.79 | 1.58 | 12.96 | 100 | 24.13 | 75.43 | 7363 |
4 | 87.23 | 92.0 | 89.5 | 100 | 0.90 | 10.70 | 14.67 | 100 | 21.44 | 75.40 | 7363 |
5 | 88.10 | 95.8 | 91.9 | 100 | 0.94 | 10.29 | 14.95 | 100 | 22.37 | 66.10 | 7363 |
從上表的結果能夠看出,經過率最高的是序號5,也就是min tarlevel = 10,target sdk level = 14,target level 11,12,13 是不推薦的版本(連谷歌都不推薦)。
如今用戶所用的版本大都是>4.0的,因此使用target level = 14是不錯的選擇。
有不少的app在使用的時候須要系統資源,好比聯網,好比GPS等等,那麼這些都是怎麼實現的呢?
很簡單隻要使用android.provider.Settings這個類就能夠了。
好比須要打開GPS操做:
1
2
|
Intent settingsIntent =
new
Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(settingsIntent);
|
以此類推。