Unity自動打包Apk


unity打包apk相對來說比較容易,相信出過的人都明白,出包過程,沒有大的難度,一步一操作,一步一等待,繁瑣耗時,不懂的人又代替不了。這時候需求就來了,如何簡單的一鍵打包搞定,這個就稍微有點難度,當然作為程序員就是要解決這些問題,封裝變化,變繁為簡。

打包apk大概可以分為以下步驟(出apk需要的jdk,Android sdk這些不用多說,相信大家都會配置)
1、配置PlayerSetting 
2、配置渠道等第三方SDK
3、copy外部資源和一些自己工程需要的一些配置
4、unity打包build(如果出成Android工程,還需要就行Android工程的自動打包apk)
沒有什么難的東西,都是一些配置,繁瑣的東西,一鍵化也就是寫個腳本,按照步驟一步一步的執行就行了
很簡單相信大家都會,只是一些細節需要注意一下,細節中還是有一些坑的
 
一、配置PlayerSetting 
 有兩種方法
1、事先使用PlayerSetting 編輯頁面保存一個配置好的ProjectSettings.asset,需要的時候直接替換掉ProjectSettings目錄下的ProjectSettings.asset就行了
設置分離obb文件的屬性,有的人可能不知道
2、使用代碼設置PlayerSetting(凡是在PlayerSetting編輯頁面看到的屬性都是可以使用代碼進行設置的)
 (1)、Unity提供了命令行方式進行打包、調用方法等操作
command line形式: unity執行文件路徑 -projectPath 項目工程路徑 -executeMethod 方法名 各種參數
在unity項目Editor下的一個繼承ScriptableObject的文件中的static方法都可以使用unity command line執行(注意Unity需要是關閉的)
$unity_path -projectPath $project_path -quit -batchmode -executeMethod ProjectBuild.ProjectSetting -qmplatform $platform 
//$unity_path unity執行文件路徑 //$project_path 項目工程路徑
//-batchmode 不打開unity //-executeMethod 執行unity方法 //ProjectBuild.ProjectSetting Editor下的腳本名為ProjectBuild,其中有個名為ProjectSetting的方法,具體代碼如下 //-qmplatform $platform 傳入的參數

 (2)、有一些屬性設置,比較難找,使用 PlayerSettings.SetPropertyInt("ScriptingBackend", (int)ScriptingImplementation.Mono2x, BuildTarget.iPhone); 慢慢嘗試

 1   class ProjectBuild : Editor
 2     {
 3         const string OPT_PLATFORM = "-qmplatform";
 4         const string OPT_BUNDLE_VERSION = "-qmbundle_version";
 5         const string OPT_SHOW_VERSION = "-qmshow_version";
 6         const string OPT_VERSION_CODE = "-qmversion_code";
 7         /// <summary>
 8         /// 根據參數配置Unity ProjectSetting
 9         /// </summary>
10         static void ProjectSetting()
11         {
12             Dictionary<string, string> settings = new Dictionary<string, string>();
13             settings[OPT_PLATFORM] = "";
14             settings[OPT_BUNDLE_VERSION] = "";
15             settings[OPT_SHOW_VERSION] = "";
16             settings[OPT_VERSION_CODE] = "";
17             string[] args = System.Environment.GetCommandLineArgs();
18             ParseArgs(settings, args);//解析參數
19 
20             PlayerSettings.companyName = "****";
21             PlayerSettings.bundleVersion = settings[OPT_BUNDLE_VERSION];
22             PlayerSettings.shortBundleVersion = settings[OPT_SHOW_VERSION];
23             PlayerSettings.defaultInterfaceOrientation = UIOrientation.AutoRotation;
24             PlayerSettings.allowedAutorotateToLandscapeLeft = true;
25             PlayerSettings.allowedAutorotateToLandscapeRight = true;
26             PlayerSettings.strippingLevel = StrippingLevel.StripByteCode;
27             PlayerSettings.aotOptions = "nimt-trampolines=512,ntrampolines=2048";
28             PlayerSettings.targetGlesGraphics = TargetGlesGraphics.Automatic;
29             PlayerSettings.Android.preferredInstallLocation = AndroidPreferredInstallLocation.Auto;
30             PlayerSettings.Android.targetDevice = AndroidTargetDevice.FAT;
31             PlayerSettings.Android.minSdkVersion = AndroidSdkVersions.AndroidApiLevel9;
32             PlayerSettings.Android.forceInternetPermission = true;
33             PlayerSettings.Android.forceSDCardPermission = true;
34             PlayerSettings.Android.bundleVersionCode = int.Parse(settings[OPT_VERSION_CODE]);
35             PlayerSettings.Android.useAPKExpansionFiles = true;//是否使用obb分離模式
36             PlayerSettings.productName = "******";
37             PlayerSettings.bundleIdentifier = "*****";
38             PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android, "宏定義");//宏定義的設置
39             EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTarget.Android);
40         }
41     }

(3)、對於一些如Icon、splash頁面的設置可以使用原名替換圖片的方法

(4)、 PlayerSettings.Android.useAPKExpansionFiles = true;//是否使用obb分離模式,這個主要是針對Google play的

obb的命名,是需要一定的規則的,不然無法使用,可以使用代碼自動設置obb的名字在Editor目錄下有這個方法,出包時會自動調用,可以在這里設置
(如果需要對dll文件進行加密的話,肯定是出成了Android工程,這里可以獲取Android工程中的dll文件進行加密,同時替換可以解密的so文件)
  [PostProcessBuild(90)]
public static void OnPostProcessBuild(BuildTarget target, string pathToBuiltProject)
{
    files = Directory.GetFiles(pathToBuiltProject, "*", SearchOption.AllDirectories);
    if (files != null)
    {
          foreach (string fileName in files)
          {
               if (fileName.Contains(".obb"))
               {
                   int bundleVersion = PlayerSettings.Android.bundleVersionCode;
                   string bundlePackageName = PlayerSettings.bundleIdentifier;
                   Directory.Move(fileName, pathToBuiltProject + "/main." + bundleVersion + "." + bundlePackageName + ".obb");
               }
           }
    }
}

 obb的使用上的還有一點小小的坑,Google play商店在下載游戲的時候,會自動下載apk所需的obb,但是有時候也不能保證一定成功,所以需要項目本身就具有這樣的檢測下載功能https://www.assetstore.unity3d.com/en/#!/content/3189這里有非常詳細的代碼,可以copy直接使用

二、配置渠道等第三方SDK和copy外部資源
1、sdk的接入
統一接口,支持不同各種渠道的sdk接入
http://blog.csdn.net/chenjie19891104/article/details/42217281這個可以看一下
2、和不同渠道綁定的參數,建議拿出來做配置,可以不用重新出版本就可以切換不同渠道的版本
3、sdk切換相關目錄資源的替換,unity直接出成apk的話,就在unity打包之前,如果是先出Android工程的話,也可以在Android工程中進行,效率會更快一點,不需要再從unity build就可以出成不同的渠道包 
 
三、unity打包build
unity build Android 可以直接打包成apk或是Android工程,使用命令行調用的方式跟上面ProjectSetting的類似,-executeMethod 的參數名換成相應函數名即可,
static void Build()
 {
           string android_project_path = "";//目標目錄
            BuildTarget target = BuildTarget.Android;
            string[] outScenes = GetBuildScenes();//需要打包的scene名字數組
            BuildPipeline.BuildPlayer(outScenes , android_project_path, target, BuildOptions.AcceptExternalModificationsToPlayer);
 }
// BuildOptions.AcceptExternalModificationsToPlayer
表示出成Android工程
直接打包apk就不用多說了,build成功就行了
 
出成Android工程后還需要幾步才能完成
1、Android工程,自動打包apk,需要配置ant環境

到官方主頁http://ant.apache.org下載新版(目前為Ant1.9.6)的ant,得到的是一個apache-ant-1.9.6-bin.zip的壓縮包。將其解壓到你的硬盤上,例如:C:\apache-ant-1.9.6。然后配置環境變量

ANT_HOME    C:/ apache-ant-1.9.6

path             C:/ apache-ant-1.9.6/bin

classpath      C:/apache-ant-1.9.6/lib

2、可以在Android工程中進行資源和渠道SDK的替換(也就是目錄文件的替換),這樣可以一個工程出成不同的渠道包
3、簽名的需要的ant.properties,這里寫着Android簽名文件的目錄,用戶名和密碼,需要事先寫好,copy到Android工程目錄下

    key.store=./config/xxx.keystore

    key.alias=xxx

    key.store.password=xxx

    key.alias.password=xxx

4、使用Android command line生成build.xml(這是ant簽名打包的必須文件) 
command line形式:  android update project -p Android工程路徑 -n 生成apk的名字(這個command line的使用需要配置一下環境變量)
android update project -n $apk_name -t 1 -p "$android_pj_path"
//$apk_name  生成apk的名字
//"$android_pj_path"  Android工程路徑

5、使用ant release,這個command line需要在Android工程目錄下執行,不然command line會找不到路徑,最后在bin目錄下生成apk文件,有簽名的和不簽名的

可以考慮與Jenkin結合使用,會更方便操作

 

(by sht)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM