unity多渠道打包

这里写自定义目录标题

  • unity多渠道打包工具

unity多渠道打包工具

最近公司叫我写一个unity多渠道打包的工具,方便他们使用,因为要每个渠道配置不太一样,只能用代码来写方便使用。
1.
#这是我的渠道配置文件build.json
unity多渠道打包_第1张图片

2.然后还要去相应的渠道下载那些SDK回来,像googleplay sdk,直接百度搜就有一大把的,再导入项目。
3.在unity创建Editor文件,再新建一个脚本MyEditor
unity多渠道打包_第2张图片
4.MyEditor脚本

using UnityEditor;
using System.Collections;
using UnityEngine;
using System.IO;
using Newtonsoft.Json;
using System.Collections.Generic;

public class MyEditor
{
    //得到项目所有场景名称
    static string[] scenesNameArr = FindEnabledEditorScenes();

    static string[] secnesName;

    //googleplay打包
    [MenuItem("多渠道打包/Build Android googleplay")]
    static void BuildAndroidKGp()
    {
        BulidTarget("googleplay", "Android");
    }

    //huwei打包
    [MenuItem("多渠道打包/Build Android huwei")]
    static void BuildAndroidHuwei()
    {
        BulidTarget("huwei", "Android");
    }

    //批量打包apk包
    [MenuItem("多渠道打包/Build ALL Android")]
    static void BuildAllAndroid()
    {
        BulidTarget("googleplay", "Android");
        BulidTarget("huwei", "Android");
    }

    //苹果Xcode工程打包
    [MenuItem("多渠道打包/Build iPhone XcodeProj")]
    static void PerformiPhoneQQBuild()
    {
        BulidTarget("XcodeProj", "IOS");
    }


    //这里封装了一个简单的通用方法。
    static void BulidTarget(string channel, string target)
    {
        for (int i = 0; i < scenesNameArr.Length; i++)
        {
            if (scenesNameArr[i] == "Main")
            {
                secnesName[0] = scenesNameArr[i];
            }
        }
        string app_name = channel + "_" + Application.companyName;
        string outTarget_dir = Application.dataPath + "/OutAndroid";
        string target_name = app_name + ".apk";
        BuildTargetGroup targetGroup = BuildTargetGroup.Android;
        BuildTarget buildTarget = BuildTarget.Android;


        string applicationPath = Application.dataPath.Replace("/Assets", "");


        if (target == "Android")
        {
            outTarget_dir = applicationPath + "/OutAndroid";
            target_name = app_name + ".apk";
            targetGroup = BuildTargetGroup.Android;
        }
        if (target == "IOS")
        {
            outTarget_dir = applicationPath + "/OutIOS";
            target_name = app_name;
            targetGroup = BuildTargetGroup.iOS;
            buildTarget = BuildTarget.iOS;
        }

        //每次build之前删除的文件
        if (Directory.Exists(outTarget_dir))
        {
            if (File.Exists(target_name))
            {
                File.Delete(target_name);
            }
            Debug.Log("=exists="+outTarget_dir);
        }
        else
        {
            Directory.CreateDirectory(outTarget_dir);
            Debug.Log("=noexists="+outTarget_dir);
        }
        //build.json是存放不同渠道的数据配置
        FileStream StreamFile = new FileStream(Application.dataPath + "/Resources/build.json", FileMode.Open);
        StreamReader StreamReader = new StreamReader(StreamFile);
        string data = StreamReader.ReadToEnd();
        BuildData bd= JsonConvert.DeserializeObject<BuildData>(data);
        //记得关闭,要不然有可能打包第2次会出现资源占用问题
        StreamFile.Close();
        StreamReader.Close();
        Debug.Log(bd.path+"===="+channel);
        //==================这里我是要读到相应的数据,然后再设置打包apk的bundleID以及对应相应代码=======================
        foreach (var item in bd.channels)
        {
            if (channel==item.channel)
            {
                Debug.Log("=="+channel);
                PlayerSettings.applicationIdentifier = item.packagename;
                PlayerSettings.bundleVersion = item.version;
                PlayerSettings.Android.bundleVersionCode = item.code;
                string[] art = item.architectures.Split(',');
                if (art.Length>0)
                {
                    for (int i = 0; i < art.Length; i++)
                    {
                        PlayerSettings.Android.targetArchitectures =(AndroidArchitecture)int.Parse(art[i]);
                    }
                }
                PlayerSettings.SetScriptingDefineSymbolsForGroup(targetGroup, item.script_define);
                PlayerSettings.Android.keystoreName = item.keystore;
                PlayerSettings.Android.keyaliasPass = item.pw;
                PlayerSettings.Android.keystorePass = item.pw;
                PlayerSettings.keystorePass = item.pw;
                break;
            }
        }
        CommonBuild(scenesNameArr, outTarget_dir + "/" + target_name, targetGroup, buildTarget, BuildOptions.None);
    }

    static string[] FindEnabledEditorScenes()
    {
        List<string> EditorScenes = new List<string>();
        foreach (EditorBuildSettingsScene scene in EditorBuildSettings.scenes)
        {
            if (!scene.enabled) continue;
            EditorScenes.Add(scene.path);
        }
        return EditorScenes.ToArray();
    }

    static void CommonBuild(string[] scenes, string dir,BuildTargetGroup buildTargetGroup, BuildTarget build_target, BuildOptions build_options)
    {
        EditorUserBuildSettings.SwitchActiveBuildTarget(buildTargetGroup,build_target);
        BuildPipeline.BuildPlayer(scenes, dir, build_target, build_options);
    }

}

5.然后返回unity点击多渠道打包,Build Android googleplay就完成了
unity多渠道打包_第3张图片
简单吧,收工罗!第一次写有什么不懂可以留言我改进,谢谢!

你可能感兴趣的:(unity多渠道打包)