Unity3d项目工程批量打包

Unity3d项目工程批量打包
当我们的一个项目工程结束的时候,常常需要很多不同分发渠道的apk,如果直接手动单个apk时,会比较麻烦。我们可以利用Unity扩展编辑器的功能来做。

 首先,在项目工程文件目录Asset文件夹下,创建Editor文件夹


其次,在其下创建一个关于批量打包的脚本,过一会儿,你会在Unity工具栏上看见“Custom”菜单栏,可以点击其下对应打包按钮,生成不同的apk。另外,值得注意的是,ios只能生成对应的Xcode工程。




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

public class MyEditorScript
{
    //得到工程中所有场景名称
    static string[] SCENES = FindEnabledEditorScenes();

    //static string[] Target_SCENES;


    //一系列批量build的操作

    //快易典apk打包
    [MenuItem("Custom/Build Android KYD")]
    static void PerformAndroidKYDBuild()
    {
        BulidTarget("KYD", "Android");
    }

    //360apk打包
    [MenuItem("Custom/Build Android 360")]
    static void PerformAndroid360Build()
    {
        BulidTarget("360", "Android");
    }

    //腾讯apk打包
    [MenuItem("Custom/Build Android TENCENT")]
    static void PerformAndroidTENCENTBuild()
    {
        BulidTarget("TENCENT", "Android");
    }

    //百度apk打包
    [MenuItem("Custom/Build Android BAI_DU")]
    static void PerformAndroidBAIDUBuild()
    {
        BulidTarget("BAI_DU", "Android");
    }

    //批量打包apk包
    [MenuItem("Custom/Build Android ALL")]
    static void PerformAndroidALLBuild()
    {
        BulidTarget("KYD", "Android");
        BulidTarget("360", "Android");
        BulidTarget("TENCENT", "Android");
        BulidTarget("BAI_DU", "Android");
    }



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




    //这里封装了一个简单的通用方法。
    static void BulidTarget(string name, string target)
    {
        //for (int i = 0; i < SCENES.Length; i++)
        //{
        //    if (SCENES[i] == "Main_Elephant")
        //    {
        //        Target_SCENES[0] = SCENES[i];
        //    }
        //}
        string app_name = name;
        string target_dir = Application.dataPath + "/TargetAndroid";
        string target_name = app_name + ".apk";
        BuildTargetGroup targetGroup = BuildTargetGroup.Android;
        BuildTarget buildTarget = BuildTarget.Android;

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

        if (target == "Android")
        {
            target_dir = applicationPath + "/TargetAndroid";
            target_name = app_name + ".apk";
            targetGroup = BuildTargetGroup.Android;
        }
        if (target == "IOS")
        {
            target_dir = applicationPath + "/TargetIOS";
            target_name = app_name;
            targetGroup = BuildTargetGroup.iOS;
            buildTarget = BuildTarget.iOS;
        }

        //每次build删除之前的残留
        if (Directory.Exists(target_dir))
        {
            if (File.Exists(target_name))
            {
                File.Delete(target_name);
            }
        }
        else
        {
            Directory.CreateDirectory(target_dir);
        }



        //==================这里是比较重要的东西,设置打包apk的bundleID以及区分宏定义调用相应代码=======================
        switch (name)
        {
            case "KYD":
                PlayerSettings.bundleIdentifier = "com.hp.picturebook_elephant";
                PlayerSettings.bundleVersion = "1.0";
                PlayerSettings.SetScriptingDefineSymbolsForGroup(targetGroup, "KYD");
                break;
            case "360":
                PlayerSettings.bundleIdentifier = "com.hp.picturebook_elephant";
                PlayerSettings.bundleVersion = "1.0";
                PlayerSettings.SetScriptingDefineSymbolsForGroup(targetGroup, "QI_HU");
                break;
            case "TENCENT":
                PlayerSettings.bundleIdentifier = "com.hp.picturebook_elephant";
                PlayerSettings.bundleVersion = "1.0";
                PlayerSettings.SetScriptingDefineSymbolsForGroup(targetGroup, "TENCENT");
                break;
            case "BAI_DU":
                PlayerSettings.bundleIdentifier = "com.hp.picturebook_elephant";
                PlayerSettings.bundleVersion = "1.0";
                PlayerSettings.SetScriptingDefineSymbolsForGroup(targetGroup, "BAI_DU");
                break;

        }

        //==================这里是比较重要的东西=======================

        //开始Build场景,等待吧~
        //注意:打包成功的apk包可以在项目工程的根目录下可以看到
        
        GenericBuild(SCENES, target_dir + "/" + target_name, buildTarget, BuildOptions.None);

        

    }

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

    static void GenericBuild(string[] scenes, string target_dir, BuildTarget build_target, BuildOptions build_options)
    {
        EditorUserBuildSettings.SwitchActiveBuildTarget(build_target);
        string res = BuildPipeline.BuildPlayer(scenes, target_dir, build_target, build_options);

        if (res.Length > 0)
        {
            throw new Exception("BuildPlayer failure: " + res);
        }
    }

}
 
  

你可能感兴趣的:(Unity)