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)
        {
Unity打包WebGL时,你可以通过编写自定义的JavaScript库(.jslib)来获取浏览器的Cookie。下面是一个简单的示例: 首先,在Unity项目中创建一个名为"Plugins"的文件夹(如果没有的话)。 然后,创建一个新的JavaScript文件,比如"CookieUtils.js",将以下代码粘贴到文件中: ```javascript mergeInto(LibraryManager.library, { GetCookieValue: function(cookieName) { var cookieValue = null; var cookies = document.cookie.split(';'); for (var i = 0; i < cookies.length; i++) { var cookie = cookies[i].trim(); if (cookie.indexOf(cookieName + '=') === 0) { cookieValue = cookie.substring(cookieName.length + 1, cookie.length); break; } } return cookieValue; } }); ``` 这段代码定义了一个名为"GetCookieValue"的函数,用于获取指定名称的Cookie的值。 保存文件,并将其放入"Plugins"文件夹中。 接下来,在C#脚本中调用这个JavaScript函数。创建一个新的C#脚本,比如"CookieManager.cs",并在其中添加以下代码: ```csharp using UnityEngine; using System.Runtime.InteropServices; public class CookieManager : MonoBehaviour { [DllImport("__Internal")] private static extern string GetCookieValue(string cookieName); void Start() { string cookieValue = GetCookieValue("yourCookieName"); Debug.Log("Cookie value: " + cookieValue); } } ``` 这段代码使用了DllImport特性,用于从JavaScript库中调用"GetCookieValue"函数。 请确保将"yourCookieName"替换为你想要获取的Cookie的名称。 现在,将"CookieManager"脚本附加到一个游戏对象上,并运行WebGL构建。 在浏览器的开发者控制台或Unity的控制台中,你应该能够看到输出的Cookie值。 这样,你就可以通过.jslib获取浏览器的Cookie了。请注意,这只适用于WebGL平台。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值