by Aklan
ZeaLotSean Modify
转载请注明.
多渠道sdk自动打包工具
ApkBuilder.cs
using GameResBuild;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;
public class ApkBuilder : EditorBase
{
public string localAndroidSDKPath = Application.dataPath + "/Plugins/Android";
public int columnCount = 5;
/// <summary>
/// 是否打包带AssetBundle的
/// </summary>
private bool m_bAssetBundleBuild = false;
[MenuItem("Build/ZeaLot's MakeApp/ApkBuilder")]
private static void DrawWindow()
{
ApkBuilder window = (ApkBuilder)EditorWindow.GetWindow(typeof(ApkBuilder));
}
private void OnGUI()
{
AddTitleLabel("BuildConfigEditor, by Aklan\r\nZeaLot Modify");
if (!OnShow)
{
return;
}
EditorGUILayout.Space();
EditorGUILayout.Space();
EditorGUILayout.BeginHorizontal();
LeftNameLabel("单行最大显示数量");
columnCount = EditorGUILayout.IntField(columnCount, GUILayout.Width(fieldWidth / 2));
EditorGUILayout.EndHorizontal();
EditorGUILayout.Space();
EditorGUILayout.Space();
m_bAssetBundleBuild = EditorGUILayout.Toggle("是否打AssetBundle包", m_bAssetBundleBuild);
EditorGUILayout.Space();
EditorGUILayout.Space();
LeftNameLabel("所有渠道打包");
if (GUILayout.Button("打包所有渠道", GUILayout.Width(btnWidth)))
{
BuildAllApk();
}
EditorGUILayout.Space();
EditorGUILayout.Space();
LeftNameLabel("单个渠道打包");
int index = 0;
foreach (var iter in BuildConfigDic)
{
if (index % columnCount == 0)
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button(iter.Value.channelName, GUILayout.Width(btnWidth)))
{
Debug.Log("Start build single apk");
if (EditorUtility.DisplayDialog("确认打包", "是否执行_" + iter.Value.channelName + "_打包?", "Yes", "No"))
{
BuildSingleApk(iter.Value.channelName);
}
// ShowBuildPath();
}
if (index % columnCount == (columnCount - 1))
{
EditorGUILayout.EndHorizontal();
}
index++;
}
}
/// <summary>
/// 打包完成后打开目录
/// </summary>
private void ShowBuildPath()
{
System.Diagnostics.Process.Start(CommonTools.buildPath);
}
private void BuildAllApk()
{
if (EditorUtility.DisplayDialog("确认打包", "是否执行所有渠道一键打包?", "Yes", "No"))
{
Debug.Log("Start build all apk");
List<string> buildList = BuildConfigDic.Keys.ToList();
for (int i = 0; i < buildList.Count; i++)
{
BuildSingleApk(buildList[i]);
}
// ShowBuildPath();
}
}
private void BuildSingleApk(string channelName)
{
BuildConfig _config;
BuildConfigDic.TryGetValue(channelName, out _config);
if (_config == null)
{
return;
}
SaveBuildValue(_config);
AssetsCopyDirectory(_config.androidSDKPath, localAndroidSDKPath);
// 调用我们自己的打包
// BuildToAndroid(config);
if (m_bAssetBundleBuild)
{
BuildAssetBundle.OneKeyMakApp();
}
else
{
BuildAssetBundle.BuildPlayerUnity();
}
}
private void AssetsCopyDirectory(string fromPath, string toPath)
{
Debug.LogWarning("Del_" + toPath + ":" + FileUtil.DeleteFileOrDirectory(toPath));
AssetDatabase.Refresh();
if (!Directory.Exists(fromPath))
{
return;
}
if (!Directory.Exists(toPath))
{
Directory.CreateDirectory(toPath);
}
string[] files = Directory.GetFiles(fromPath + "/");
foreach (string file in files)
{
string name = file.Substring(file.LastIndexOf("/"));
FileUtil.CopyFileOrDirectory(file, toPath + "/" + name);
}
string[] dirs = Directory.GetDirectories(fromPath + "/");
foreach (string dir in dirs)
{