1. 程式人生 > >Unity3D安卓打包引數配置與相容性的關係分析

Unity3D安卓打包引數配置與相容性的關係分析

http://www.gopedu.com/article/706

1.前言

       在使用Unity3D工程匯出安卓安裝包的時候,往往會遇到相容性的問題,針對某些機型,要麼無法開啟遊戲,要麼會出現卡機的現象。面對這種情況,我們可以調節相關的引數來提高相容性。

       為了瞭解在打包時候每個引數的含義,我們查找了Unity3D的官方文件。為了清楚每個引數所起的作用以及每個引數對相容性的影響因素,我們有針對性的做了對比實驗,然後在安卓雲測試平臺(http://www.testin.cn)測試相容性,將最終結果進行比較,最後提出優化方案。

2.文件說明

功能介紹

       Unity4.5安卓打包引數配置主要講述Unity4.5在進行安卓打包時遇到的一些引數,這些引數的詳細解釋,以及如何通過修改引數來提高遊戲的相容性。本文主要描述如何配置打包引數,供Unity4.5輸出安卓包的工作人員使用。

閱讀物件

        本文件面向瞭解Android,Unity3D客戶端的開發人員。

3.配置引數詳解

一.Rendering Path(This property is sharedbetween Standalone and WebPlayer content)

      渲染路徑,這個屬性一般在PC上或者網頁上比較有用,一般有如下三個選項

       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)

      對光的特徵支援的很好,但是對陰影支援的不好

       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

      是否啟用動態度量,預設是啟用轉檯。

4.實驗資料

實驗方法

      該實驗採取對照實驗的方法。如果要考察單個引數對於相容性的影響,必須得控制另外的引數不變。然後針對不同的配置引數打包,上傳到雲測試平臺進行相容性測試。

5.實驗操作流程

對要進行測試的引數進行設計。

主要涉及到如下幾個引數

      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 =newIntent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivity(settingsIntent);