打包是程序员必经之路。如何实现自动化是一个程序员的基本素质。(都是因为我懒,哈哈哈)。
使用环境 :unity3D 2017.4.7 Mac Pro
打包分一下几个部分 :
1、unity3D导出xcode 工程
2、设置xcode配置及各种sdk配置
3、命令行打包
4、上传蒲公英
5、上传bugly
【IOS自动化解放双手系列一】unity3D导出xcode 工程并配置各种权限
【IOS自动化解放双手系列二】命令行打IPA包和上传蒲公英
【IOS自动化解放双手系列三】上传bugly
接下来开始正式看代码
1、unity3D导出xcode 工程
我们使用assetbundle打包所以需要先打ab包 让配置相关服务器文件,在进行导出 代码如下
public enum BUILDTYPE {
QA,
PREFE,
REALSE
}
private static BUILDTYPE currenBulidType = BUILDTYPE.REALSE;
public static void ExportIOSProject(BUILDTYPE uILDTYPE) {
currenBulidType = uILDTYPE;
EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTargetGroup.iOS, BuildTarget.iOS);
AssetDatabase.Refresh();
MoveGameConfig(uILDTYPE);
string shellPath = Application.dataPath.Replace("Assets", "Build/iOS/");
//step1.打包资源。通过AssetBundleGraph 生成Build AssetsBundles
BuildAssetsBundles();
UnityEngine.Debug.Log("step1 Finished!!!");
//Step2.移除无关文件,为Export IOS 工程做准备,调用shell脚本
RunShell(shellPath + "RemoveFromUnity.sh");
AssetDatabase.Refresh();
UnityEngine.Debug.Log("step2 Finished!!!");
//Step3.导出Apple 真机工程
ExportAppleProject(iOSSdkVersion.DeviceSDK);
UnityEngine.Debug.Log("step3 Finished!!!");
//Step4.删除临时文件,调用shell脚本
RunShell(shellPath + "RecoverUnity.sh");
UnityEngine.Debug.Log("step4 Finished!!!");
PlayerSettings.applicationIdentifier = "com.XXXX.XXX";
AssetDatabase.Refresh();
}
public static void ExportAppleProject(iOSSdkVersion sdk) {
List<string> scenes = new List<string>();
foreach (EditorBuildSettingsScene scene in EditorBuildSettings.scenes) {
if (!scene.enabled)
continue;
scenes.Add(scene.path);
}
bool isQA = false;
switch (currenBulidType) {
case BUILDTYPE.QA:
case BUILDTYPE.PREFE:
isQA = true;
PlayerSettings.applicationIdentifier = "com.XXXX.XXX";
break;
case BUILDTYPE.REALSE:
PlayerSettings.applicationIdentifier = "com.XXXX.XXX";
isQA = false;
break;
}
EditorUserBuildSettings.development = isQA;
PlayerSettings.iOS.sdkVersion = sdk;
PlayerSettings.iOS.buildNumber = GetVersion().ToString();
//EditorUserBuildSettings.target
BuildOptions buildOption = BuildOptions.None;
string res = BuildPipeline.BuildPlayer(scenes.ToArray(), PathUtils.CombinePath("Build", ToolsFunctions.GetPlatformDefines(), "Project", sdk.ToString()), BuildTarget.iOS, buildOption);
if (res.Length > 0) {
throw new Exception("BuildPlayer failure : " + res);
}
}
//=====================================公共函数=======================================
private static void BuildAssetsBundles() {
UnityEngine.AssetBundles.GraphTool.BatchBuildWindow.Open();
UnityEngine.AssetBundles.GraphTool.BatchBuildWindow.BuildFromMenu();
}
private static void RunShell(string shellFile) {
// Thread thread = new Thread(new )
//为了可以重试多线程所以加了一层
UnityRunShell(shellFile);
}
private static void UnityRunShell(string shellFile) {
Process process = new Process();
process.StartInfo.FileName = shellFile;
// process.StartInfo.Arguments = "";
process.StartInfo.UseShellExecute = false;
process.StartInfo.ErrorDialog = true;
process.StartInfo.CreateNoWindow = false;
process.Start();
process.WaitForExit();
process.Close();
}
private static int GetVersion(){
return ClientVersion.GetIncrementBundleVersion();
}
private static void MoveGameConfig(BUILDTYPE uILDTYPE ){
string path = PathUtils.CombinePath(Application.dataPath,"../GameConfig/gameRealse.xml");
switch(uILDTYPE){
case BUILDTYPE.QA:
path = PathUtils.CombinePath(Application.dataPath,"../GameConfig/gameQA.xml");
break;
case BUILDTYPE.PREFE:
path = PathUtils.CombinePath(Application.dataPath,"../GameConfig/gamePrefe.xml");
break;
case BUILDTYPE.REALSE:
path = PathUtils.CombinePath(Application.dataPath,"../GameConfig/gameRealse.xml");
break;
}
CopyConfig(path);
}
private static void CopyConfig(string path) {
string targePath = PathUtils.CombinePath(PathUtils.GameConfigWWWPath, "game.xml");
string configStr = FileUtils.ReadAllText(path);
if (string.IsNullOrEmpty(configStr)) {
Client.LogManager.LogError("配置信息有误请查看后重新写入");
}
FileUtils.WriteAllText(targePath, configStr);
Client.LogManager.Log("copy GameConfig finish");
}
2、设置xcode配置及各种sdk配置
分一下几部分:
添加类库
框架的搜索追加设置
设置推送权限配置XXXXX.entitlements文件
unity修改oc代码
#if (UNITY_IOS || UNITY_IPHONE)
using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System;
using System.Linq;
using System.Diagnostics;
using CDBG