環境
Unity5.3.2f1にて動作確認
前書き
開発用・テスト用・リリース用などビルドの設定を手動で変更するのが面倒だったのでパターンを定義して一発で出力する方法を調べました。
やりたいこと
- ビルドを出力したい > BuildPipeline.BuildPlayerを使う
- ビルド時のオプションを変更したい > BuildOptionsを使う
- シンボル定義を変更したい > PlayerSettings.SetScriptingDefineSymbolsForGroupを使う
サンプル
Standaloneビルド用のサンプルです。
CustomBuild.cs
using UnityEngine;
using UnityEditor;
using System.Linq;
using System;
public class CustomBuild : MonoBehaviour {
enum BuildPattern {
RELEASE,
TEST,
DEV
}
const string BUILD_SYMBOL_FOR_RELEASE = "RELEASE";
const string BUILD_SYMBOL_FOR_TEST = "TEST";
const string BUILD_SYMBOL_FOR_DEV = "DEV";
const BuildOptions BUILD_OPTION_FOR_RELEASE = BuildOptions.None;
const BuildOptions BUILD_OPTION_FOR_TEST = BuildOptions.AllowDebugging;
const BuildOptions BUILD_OPTION_FOR_DEV = BuildOptions.AllowDebugging | BuildOptions.Development;
[MenuItem("Build/Release")]
static void DoCustomBuildForRelease() { OutputBuild(BuildPattern.RELEASE); }
[MenuItem("Build/Test")]
static void DoCustomBuildForTest() { OutputBuild(BuildPattern.TEST); }
[MenuItem("Build/Dev")]
static void DoCustomBuildForDev() { OutputBuild(BuildPattern.DEV); }
[MenuItem("Build/ALL")]
static void DoCustomBuildALL() { foreach(BuildPattern p in Enum.GetValues(typeof(BuildPattern))) OutputBuild(p); }
static BuildOptions GetBuildOptions(BuildPattern pattern) {
switch (pattern) {
case BuildPattern.RELEASE: return BUILD_OPTION_FOR_RELEASE;
case BuildPattern.TEST: return BUILD_OPTION_FOR_TEST;
case BuildPattern.DEV: return BUILD_OPTION_FOR_DEV;
default: throw new Exception("想定外のBuildPattern:" + pattern);
}
}
static string GetBuildSymbols(BuildPattern pattern) {
switch (pattern) {
case BuildPattern.RELEASE: return BUILD_SYMBOL_FOR_RELEASE;
case BuildPattern.TEST: return BUILD_SYMBOL_FOR_TEST;
case BuildPattern.DEV: return BUILD_SYMBOL_FOR_DEV;
default: throw new Exception("想定外のBuildPattern:" + pattern);
}
}
static void OutputBuild(BuildPattern pattern) {
// ビルドシンボル設定
PlayerSettings.SetScriptingDefineSymbolsForGroup(
BuildTargetGroup.Standalone,
GetBuildSymbols(pattern));
// ビルド出力先パス生成
string outputPath =
pattern.ToString() + "_" + DateTime.Now.ToString("yyyyMMdd_HHmm") + "/" +
PlayerSettings.productName + ".exe";
// ビルド出力
BuildPipeline.BuildPlayer(
EditorBuildSettings.scenes.Select(s => s.path).ToArray(),
"_Builds/" + outputPath,
BuildTarget.StandaloneWindows64,
GetBuildOptions(pattern)
);
}
}
あとがき
同じ要領でプラットフォームを切り替えることもできます。