unity多渠道sdk打包.

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)
        {
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值