研发了两年多的项目开始推广了,为了应对多平台、多渠道需要打包巨多的应用程序,写了个一键打包工具,下面是源码及讲解。 这个打包工具包含的功能有一下几点:
- 自动配置ProjectSetting
- 自动配置KeyStore
- 自动打包场景
- 自动配置项目某个脚本的参数(如taptap、4399等渠道)
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System;
using System.IO;
using LitJson;
namespace BuildTools
{
public class BuildTools
{
[InitializeOnLoad]
public class GlobalConfig
{
static GlobalConfig()
{
PlayerSettings.Android.keystorePass = "XXXXXX";
PlayerSettings.Android.keyaliasName = "XXX";
PlayerSettings.Android.keyaliasPass = "XXXXXX";
}
}
public class BuildJson
{
public int DownloadPlatform { set; get; }
public int GameType { set; get; }
}
public enum DownloadPlatform
{
TempTest,
TapTap,
Game4399,
Steam,
WanDouJia,
Official
}
public enum GameType
{
GameLife,
XinMo,
}
static string[] SCENES = FindEnabledEditorScenes();
[MenuItem("BuildTools/正式服/游戏人生")]
static void GameLife()
{
BulidTarget("游戏人生(4399)", GameType.GameLife, DownloadPlatform.Game4399);
BulidTarget("游戏人生(TapTap)", GameType.GameLife, DownloadPlatform.TapTap);
BulidTarget("游戏人生(官方)", GameType.GameLife, DownloadPlatform.Official);
BulidTarget("游戏人生(豌豆荚)", GameType.GameLife, DownloadPlatform.WanDouJia);
}
[MenuItem("BuildTools/正式服/心魔")]
static void XinMo()
{
BulidTarget("心魔(4399)", GameType.XinMo, DownloadPlatform.Game4399);
BulidTarget("心魔(TapTap)", GameType.XinMo, DownloadPlatform.TapTap);
BulidTarget("心魔(官方)", GameType.XinMo, DownloadPlatform.Official);
BulidTarget("心魔(豌豆荚)", GameType.XinMo, DownloadPlatform.WanDouJia);
}
static BuildJson buildJson = new BuildJson();
static void BulidTarget(string apkName, GameType gameType, DownloadPlatform DownloadPlatform)
{
buildJson.DownloadPlatform = (int)DownloadPlatform;
buildJson.GameType = (int)gameType;
WriteLine();
string app_name = apkName;
string target_dir = "E:/APK_正式服";
string target_name = app_name + ".apk";
string ScriptingDefineSymbols = "VUFORIA_SAMPLE_ORIENTATION_SETTINGS;VUFORIA_ANDROID_SETTINGS;CROSS_PLATFORM_INPUT;MOBILE_INPUT;";
BuildTargetGroup targetGroup = BuildTargetGroup.Android;
BuildTarget buildTarget = BuildTarget.Android;
if (Directory.Exists(target_dir))
{
if (File.Exists(target_name))
{
File.Delete(target_name);
}
}
else
{
Directory.CreateDirectory(target_dir);
}
switch (gameType)
{
case GameType.GameLife:
PlayerSettings.applicationIdentifier = "com.dxzb.xinmo";
PlayerSettings.bundleVersion = "1.0";
PlayerSettings.SetScriptingDefineSymbolsForGroup(targetGroup, ScriptingDefineSymbols + "Cheng");
PlayerSettings.productName = "游戏人生";
break;
case GameType.XinMo:
PlayerSettings.applicationIdentifier = "com.dxzb.xinmose";
PlayerSettings.bundleVersion = "1.0";
PlayerSettings.SetScriptingDefineSymbolsForGroup(targetGroup, ScriptingDefineSymbols + "Cheng;BattleSplit");
PlayerSettings.productName = "心魔";
break;
case GameType.BreakThrough:
PlayerSettings.applicationIdentifier = "com.dxzb.xinmoBT.m4399";
PlayerSettings.bundleVersion = "1.0";
PlayerSettings.SetScriptingDefineSymbolsForGroup(targetGroup, ScriptingDefineSymbols + "Cheng;BattleSplit");
PlayerSettings.productName = "灵兽闯关";
break;
}
PlayerSettings.companyName = "xxx";
GenericBuild(SCENES, target_dir + "/" + target_name, 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 GenericBuild(string[] scenes, string target_dir, BuildTarget build_target, BuildOptions build_options)
{
EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTargetGroup.Android, build_target);
BuildPipeline.BuildPlayer(scenes, target_dir, build_target, build_options);
}
static void WriteLine()
{
StreamWriter writer;
string filePath = "";
filePath = Application.streamingAssetsPath + "/BuildJson.json";
FileInfo fileInfo = new FileInfo(filePath);
writer = new StreamWriter(filePath, false);
writer.Write(JsonMapper.ToJson(buildJson),false);
writer.Close();
}
}
}
|