【打包工具】

dev_chengFeng
gaoxin 2021-05-27 10:12:22 +08:00
parent e0a5d6d134
commit 49bef51ca0
2 changed files with 345 additions and 142 deletions

1
.gitignore vendored
View File

@ -29,3 +29,4 @@ obj/
Assets/ManagedResources/~Lua/Config/Data.meta
*.DS_Store
BuildABs/Android/
ExportProject/

View File

@ -6,189 +6,391 @@ using GameEditor.FrameTool;
public class AutoPack : EditorWindow
{
static string[][] PackConfig = new string[][]
{
new string[]{"喜扑", "Logo_cn_xipu", "MHT_CN_XIPU"},
new string[]{"草花", "Logo_cn_xipu", "MHT_CN_MIDDLE_WARE" },
};
#if UNITY_ANDROID
static string Platform = "Android";
static string ConfigFileName = "config.txt";
static string VersionFileName = "version.txt";
#elif UNITY_IOS
static string Platform = "IOS";
static string ConfigFileName = "config.txt";
static string VersionFileName = "game.info";
#endif
static string MomPackPath;
static string ExportPackPath;
static string ScenePath;
static string ABPath;
// 判断是否要重新导出工程
static string[] ExportTypeList =
{
"导出工程,替换出包",
"只替换AB出包",
"只出包"
};
static int ExportType;
static bool[] Chooser;
static bool isObb = false;
bool isBuild = false;
static bool isZs = false;
static string[] versionArr = new string[15];
static int verIndex = 0;
static string curProjectDir;
static string m_ExcelPath;
static string[] excelArr;
static int excelIndex=0;
private void OnEnable()
{
MomPackPath = EditorPrefs.GetString("MomPackPath", "");
ExportPackPath = Application.dataPath.Replace("Assets", "ExportProject");
ScenePath = Application.dataPath + "/LuaFramework/Scenes/";
ABPath = Application.dataPath + "/../BuildABs/" + Platform + "/";
ExportType = 2;
Chooser = new bool[PackConfig.Length];
}
/// <summary>
/// 版本文件路径
/// </summary>
static string _MomPackPath
{
get
{
return MomPackPath + "/" + Platform + "/";
}
}
//bool isBuild = false;
//static bool isZs = false;
//static string[] versionArr = new string[15];
//static int verIndex = 0;
//static string curProjectDir;
//static string m_ExcelPath;
//static string[] excelArr;
//static int excelIndex=0;
//打包数据
[MenuItem("自动化打包/一键导出")]
private static void EzBuildPack()
{
curProjectDir = Environment.CurrentDirectory.Replace('\\', '/');
verIndex = EditorPrefs.GetInt("verIndex");
m_ExcelPath = EditorPrefs.GetString("m_ExcelPath");
excelArr = Directory.GetDirectories(m_ExcelPath, "master_*");
Debug.Log(EditorPrefs.GetString("m_Bench"));
if (excelArr!=null)
{
for (int i = 0; i < excelArr.Length; i++)
{
if (excelArr[i]== EditorPrefs.GetString("m_Bench"))
{
excelIndex = i;
}
}
}
string[] _strArr = Directory.GetFiles(curProjectDir + "/AssetBundles/", "*.txt");
Debug.Log("version列表数量:"+_strArr.Length);
for (int i = 0; i < _strArr.Length; i++)
{
string[] pathSpl = _strArr[i].Split('/');
string _str = pathSpl[pathSpl.Length-1];
versionArr[i] = _str;
}
//创建窗口
Rect wr = new Rect(0, 0, 400, 500);
Rect wr = new Rect(0, 0, 500, 700);
var buildWin = GetWindowWithRect<AutoPack>(wr, true);
buildWin.titleContent = new GUIContent("打包工具");
buildWin.Show();
}
// 打开文本
private void OpenText(string tPath)
{
if (!File.Exists(tPath))
{
EditorUtility.DisplayDialog("提示", "文件不存在", "确定");
return;
}
System.Diagnostics.Process.Start("notepad.exe", tPath); //用记事本
}
private void OnGUI()
{
EditorGUILayout.BeginVertical();
EditorGUILayout.Space();
EditorGUILayout.Space();
if (excelArr!=null&& excelArr.Length>0)
//EditorGUILayout.LabelField("");
MomPackPath = EditorGUILayout.TextField("母包工程地址:", MomPackPath);
EditorGUILayout.Space();
EditorGUILayout.Space();
EditorGUILayout.LabelField("请选择要打的包:");
for(int i = 0; i < PackConfig.Length; i++)
{
excelIndex = EditorGUILayout.Popup("选择excel文件", excelIndex, excelArr);
EditorGUILayout.BeginVertical();
Chooser[i] = EditorGUILayout.ToggleLeft(PackConfig[i][0], Chooser[i]);
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("编辑Config", GUILayout.Height(20f)))
{
string tpath = _MomPackPath + PackConfig[i][2] + "/Config/" + ConfigFileName;
OpenText(tpath);
}
if (GUILayout.Button("编辑Version", GUILayout.Height(20f)))
{
string tpath = _MomPackPath + PackConfig[i][2] + "/Config/" + VersionFileName;
OpenText(tpath);
}
if (GUILayout.Button("编辑Gradle", GUILayout.Height(20f)))
{
string tpath = _MomPackPath + PackConfig[i][2] + "/build.gradle";
OpenText(tpath);
}
EditorGUILayout.EndHorizontal();
EditorGUILayout.EndVertical();
EditorGUILayout.Space();
if (GUILayout.Button("一键导表", GUILayout.Height(40f)))
EditorGUILayout.Space();
}
ExportType = EditorGUILayout.Popup("请选择出包方式", ExportType, ExportTypeList);
EditorGUILayout.Space();
EditorGUILayout.Space();
if (GUILayout.Button("开始", GUILayout.Height(40f)))
{
StartExport();
}
}
public static void StartExport()
{
if (MomPackPath.Equals(""))
{
EditorUtility.DisplayDialog("提示", "母包工程地址错误", "确定");
return;
}
bool check_result = false;
for (int i = 0; i < Chooser.Length; i++)
{
if (!Chooser[i]) continue;
string sceneFilePath = ScenePath + PackConfig[i][1];
if (!File.Exists(sceneFilePath + ".unity"))
{
EditorPrefs.SetString("m_Bench", excelArr[excelIndex]);
GameEditor.Core.DataConfig.DataConfigWindow.excelALLConfig(false, excelArr[excelIndex] + "/base_data");
Debug.LogError(PackConfig[i][0] + " 未找到场景:" + sceneFilePath);
check_result = true;
}
string momPath = _MomPackPath + PackConfig[i][2];
if (!Directory.Exists(momPath))
{
Debug.LogError(PackConfig[i][0] + " 未找到母包:" + momPath);
check_result = true;
}
}
if (GUILayout.Button("单打lua资源", GUILayout.Height(40f)))
if (check_result)
{
if (FrameTool.BuildLuaAssetBundles())
{
string exportPath = GameEditor.AssetBundle.AssetBundleConfig.GetExportPath(EditorUserBuildSettings.activeBuildTarget);
string targetPath = FrameTool.GetStreamingAssetPath(EditorUserBuildSettings.activeBuildTarget);
File.Copy(exportPath + "/lzma/luabytes.unity3d", targetPath + "/lzma/luabytes.unity3d", true);
File.Copy(exportPath + "/lzma/resconfigs.unity3d", targetPath + "/lzma/resconfigs.unity3d", true);
File.Copy(exportPath + "/files.unity3d", targetPath + "/files.unity3d", true);
}
EditorUtility.DisplayDialog("提示", "包路径错误", "确定");
return;
}
EditorGUILayout.Space();
EditorGUILayout.Space();
EditorGUILayout.Space();
EditorGUILayout.Space();
verIndex = EditorGUILayout.Popup("选择version配置文件", verIndex, versionArr);
EditorGUILayout.Space();
#if UNITY_ANDROID
isObb = EditorGUILayout.ToggleLeft("是否导出obb包", isObb);
#endif
isBuild = EditorGUILayout.ToggleLeft("是否需要build资源", isBuild);
isZs = EditorGUILayout.ToggleLeft("是否从正式场景导出", isZs);
EditorGUILayout.Space();
if (GUILayout.Button("导出工程", GUILayout.Height(40f)))
// 保存配置
EditorPrefs.SetString("MomPackPath", MomPackPath);
// 开始导出
for (int i = 0; i < Chooser.Length; i++)
{
Debug.Log("开始导出工程");
EditorPrefs.SetInt("verIndex", verIndex);
string targetPath = Application.dataPath + "/Resources/version.txt";
string copyPath = curProjectDir + "/AssetBundles/" + versionArr[verIndex];
Debug.Log("version文件路径:" + copyPath);
if (File.Exists(targetPath))
{
File.Delete(targetPath);
if (!Chooser[i]) continue;
switch (ExportType) {
case 0: // 导出工程
ExportAS(i);
ReplaceFromProject(i);
ReplaceConfigAndVersion(i);
StartGradleBuild(i);
break;
case 1: // 只替换AB
ReplaceFromAB(i);
ReplaceConfigAndVersion(i);
StartGradleBuild(i);
break;
case 2: // 只重新打包
StartGradleBuild(i);
break;
}
File.Copy(copyPath, targetPath, true);
AssetDatabase.Refresh();
if (isBuild)
{
Debug.Log("开始build资源");
//打包游戏
FrameTool.BuildGameAssetBundles();
//拷贝AssetBundle到流媒体目录
FrameTool.CopyAssetBundleToStreamingAssets();
//刷新资源配置
GameEditor.ResourcesPathEditor.CreateResourcePathConfig();
Debug.Log("完成build资源");
}
#if UNITY_ANDROID
string sceneName = "Assets/LuaFramework/Scenes/LogoHwTest.unity";
if (isZs)
{
sceneName = "Assets/LuaFramework/Scenes/LogoHwRelease.unity";
}
ExportAS( sceneName);
#elif UNITY_IOS
string sceneName = "Assets/LuaFramework/Scenes/Logo.unity";
ExportXcode(sceneName);
#endif
}
}
public static void ExportAS( string _sceneName)
// 导出android 工程
public static void ExportAS(int index)
{
PlayerSettings.productName = "ex_android_jl";
PlayerSettings.Android.useAPKExpansionFiles = isObb;
string outPath = "D:/exAndroid/unity_ex_as";
string pName = PackConfig[index][1] + "__" + PackConfig[index][2];
PlayerSettings.productName = pName;
PlayerSettings.Android.useAPKExpansionFiles = isObb;
if (isObb)
{
outPath = "D:/exAndroid/unity_ex_obb";
pName += "__obb";
}
if (Directory.Exists(outPath + "/ex_android_jl"))
if (!Directory.Exists(ExportPackPath))
{
Directory.Delete(outPath + "/ex_android_jl/", true);
Directory.CreateDirectory(ExportPackPath);
}
if (Directory.Exists(ExportPackPath + "/" + pName))
{
Directory.Delete(ExportPackPath + "/" + pName, true);
}
EditorUserBuildSettings.exportAsGoogleAndroidProject = true;
EditorUserBuildSettings.androidBuildSystem = AndroidBuildSystem.Gradle;
// 设置需要打包的场景
string launchScene = _sceneName;
Debug.Log("打包场景:" + launchScene);
BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions();
buildPlayerOptions.locationPathName = outPath;
buildPlayerOptions.options = BuildOptions.None;
buildPlayerOptions.options |= BuildOptions.AcceptExternalModificationsToPlayer;
buildPlayerOptions.scenes = new[] { launchScene };
buildPlayerOptions.target = BuildTarget.Android;
string launchScene = "Assets/LuaFramework/Scenes/" + PackConfig[index][1] + ".unity";
Debug.Log(PackConfig[index][0] + "打包场景:" + launchScene);
BuildPlayerOptions bpo = new BuildPlayerOptions();
bpo.locationPathName = ExportPackPath;
bpo.options = BuildOptions.None;
//bpo.options |= BuildOptions.AcceptExternalModificationsToPlayer;
bpo.scenes = new[] { launchScene };
bpo.target = BuildTarget.Android;
// 调用开始打包
BuildPipeline.BuildPlayer(buildPlayerOptions);
Debug.Log("android工程导出路径"+ outPath + "/ex_android_jl");
Debug.Log("导出完成");
///打开python工具所在路径
GameEditor.Core.DataConfig.ConfigExportWindow.OpenDirectory("C:/Users/aaa/Desktop/pythonPack");
BuildPipeline.BuildPlayer(bpo);
Debug.Log(PackConfig[index][0] + "android工程导出路径" + ExportPackPath + "/" + pName);
Debug.Log(PackConfig[index][0] + "android工程导出完成");
}
private static void ExportXcode(string _sceneName)
static string[] ReplaceDir = new string[]
{
PlayerSettings.productName = "太初行";
string mainXcodePath =
isZs? "/Volumes/ELEMENTS/JieLing/Packager/IOS/MHT_HW_RELEASE": "/Volumes/ELEMENTS/JieLing/Packager/IOS/MHT_HW_TEST";
string outPath = isZs?"/Volumes/ELEMENTS/JieLing/Packager/IOS/exXcodePro_Release":"/Volumes/ELEMENTS/JieLing/Packager/IOS/exXcodePro_Test";
if (Directory.Exists(outPath))
"/src/main/assets/Android",
"/src/main/assets/bin",
"/src/main/jniLibs"
};
// 从工程中替换
private static void ReplaceFromProject(int index)
{
string fromPath = ExportPackPath + "/" + PackConfig[index][1] + "__" + PackConfig[index][2];
string toPath = _MomPackPath + PackConfig[index][2];
for (int i = 0; i < ReplaceDir.Length; i++)
{
Directory.Delete(outPath, true);
}
// 设置需要打包的场景
string launchScene = _sceneName;
Debug.Log("打包场景:" + launchScene);
BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions();
buildPlayerOptions.locationPathName = outPath;
buildPlayerOptions.options = BuildOptions.None;
buildPlayerOptions.options |= BuildOptions.AcceptExternalModificationsToPlayer;
buildPlayerOptions.scenes = new[] { launchScene };
buildPlayerOptions.target = BuildTarget.iOS;
// 调用开始打包
BuildPipeline.BuildPlayer(buildPlayerOptions);
Debug.Log("打包完成");
Debug.Log("导出工程路径:"+ outPath);
if (Directory.Exists(mainXcodePath)&& Directory.Exists(outPath))
{
Debug.Log("替换xcode资源");
Directory.Delete(mainXcodePath+"/Data", true);
Directory.Move(outPath+"/Data",mainXcodePath+"/Data");
Directory.Delete(mainXcodePath+"/Classes/Native", true);
Directory.Move(outPath+"/Classes/Native",mainXcodePath+"/Classes/Native");
string dir = ReplaceDir[i];
string to = toPath + dir;
if (Directory.Exists(to)) Directory.Delete(to, true);
Directory.CreateDirectory(to);
GameCore.FileUtils.CopyDir(fromPath + dir, to);
}
Debug.Log(PackConfig[index][0] + ":母包工程资源替换完成");
// 替换build-id
RewriteManifest(fromPath + "/src/main/AndroidManifest.xml", toPath + "/src/main/AndroidManifest.xml");
Debug.Log(PackConfig[index][0] + "母包Manifest修改完成");
}
private static void RewriteManifest(string f, string t)
{
// 找到要替换的内容
string[] flines = File.ReadAllLines(f, System.Text.Encoding.UTF8);
string rpl = "";
for (int fi = 0; fi < flines.Length; fi++)
{
if (flines[fi].Contains("unity.build-id"))
{
rpl = flines[fi];
break;
}
}
// 判断是否正确
if (rpl.Equals(""))
{
return;
}
// 替换到母包中
string[] tlines = File.ReadAllLines(t, System.Text.Encoding.UTF8);
string[] wlines = new string[tlines.Length - 1];
string lastLine = "";
for (int ti = 0; ti < tlines.Length; ti++)
{
if (tlines[ti].Contains("unity.build-id"))
{
tlines[ti] = rpl;
}
if (ti == tlines.Length - 1)
{
lastLine = tlines[ti];
}
else
{
wlines[ti] = tlines[ti];
}
}
File.WriteAllLines(t, wlines, System.Text.Encoding.UTF8);
File.AppendAllText(t, lastLine, System.Text.Encoding.UTF8);
}
// 从AB包替换
private static void ReplaceFromAB(int index)
{
string toPath = _MomPackPath + PackConfig[index][2] + "/src/main/assets/Android";
if (Directory.Exists(toPath)) Directory.Delete(toPath, true);
Directory.CreateDirectory(toPath);
GameCore.FileUtils.CopyDir(ABPath, toPath);
Debug.Log(PackConfig[index][0] + "替换AB包完成");
}
// 替换配置文件
private static void ReplaceConfigAndVersion(int index)
{
string toPath = _MomPackPath + PackConfig[index][2] + "/src/main/assets/Android/";
string fromPath = _MomPackPath + PackConfig[index][2] + "/Config/";
File.Copy(fromPath + ConfigFileName, toPath + ConfigFileName, true);
File.Copy(fromPath + VersionFileName, toPath + VersionFileName, true);
Debug.Log(PackConfig[index][0]+":配置文件替换完成");
}
// 开始生成apk
private static void StartGradleBuild(int index)
{
ProcessCommand(_MomPackPath + PackConfig[index][2], new string[] { "gradlew assembleDebug" });
Debug.Log(PackConfig[index][0] + ":打包完成");
}
//
private static void ProcessCommand(string dir, string[] coms)
{
System.Diagnostics.Process p = new System.Diagnostics.Process();
//设置要启动的应用程序
p.StartInfo.FileName = "cmd.exe";
//是否使用操作系统shell启动
p.StartInfo.UseShellExecute = true;
//接受来自调用程序的输入信息
p.StartInfo.RedirectStandardInput = false;
//输出信息
p.StartInfo.RedirectStandardOutput = false;
//输出错误
p.StartInfo.RedirectStandardError = false;
//不显示程序窗口
p.StartInfo.CreateNoWindow = false;
// 设置文件夹
p.StartInfo.WorkingDirectory = dir;
// 执行命令
for(int i = 0; i < coms.Length; i++)
{
p.StartInfo.Arguments = "/k " + coms[i] + "&exit";
//启动程序
p.Start();
}
p.StartInfo.Arguments = "/k exit";
//启动程序
p.Start();
//等待程序执行完退出进程
p.WaitForExit();
p.Close();
}
//private static void ExportXcode(string _sceneName)
//{
// PlayerSettings.productName = "太初行";
// string mainXcodePath =
// isZs? "/Volumes/ELEMENTS/JieLing/Packager/IOS/MHT_HW_RELEASE": "/Volumes/ELEMENTS/JieLing/Packager/IOS/MHT_HW_TEST";
// string outPath = isZs?"/Volumes/ELEMENTS/JieLing/Packager/IOS/exXcodePro_Release":"/Volumes/ELEMENTS/JieLing/Packager/IOS/exXcodePro_Test";
// if (Directory.Exists(outPath))
// {
// Directory.Delete(outPath, true);
// }
// // 设置需要打包的场景
// string launchScene = _sceneName;
// Debug.Log("打包场景:" + launchScene);
// BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions();
// buildPlayerOptions.locationPathName = outPath;
// buildPlayerOptions.options = BuildOptions.None;
// buildPlayerOptions.options |= BuildOptions.AcceptExternalModificationsToPlayer;
// buildPlayerOptions.scenes = new[] { launchScene };
// buildPlayerOptions.target = BuildTarget.iOS;
// // 调用开始打包
// BuildPipeline.BuildPlayer(buildPlayerOptions);
// Debug.Log("打包完成");
// Debug.Log("导出工程路径:"+ outPath);
// if (Directory.Exists(mainXcodePath)&& Directory.Exists(outPath))
// {
// Debug.Log("替换xcode资源");
// Directory.Delete(mainXcodePath+"/Data", true);
// Directory.Move(outPath+"/Data",mainXcodePath+"/Data");
// Directory.Delete(mainXcodePath+"/Classes/Native", true);
// Directory.Move(outPath+"/Classes/Native",mainXcodePath+"/Classes/Native");
// }
//}
}