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

$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目錄下有這個方法,出包時會自動調用,可以在這里設置 obb的使用上的還有一點小小的坑,Google play商店在下載游戲的時候,會自動下載apk所需的obb,但是有時候也不能保證一定成功,所以需要項目本身就具有這樣的檢測下載功能https://www.assetstore.unity3d.com/en/#!/content/3189這里有非常詳細的代碼,可以copy直接使用
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工程
到官方主頁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
key.store=./config/xxx.keystore
key.alias=xxx
key.store.password=xxx
key.alias.password=xxx
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)

