特殊な例かもしれませんが、私が今作っているプロジェクトは複数パターンのビルドをしたいなということがまぁまぁあります。
しかも同じWebGL内で。
一応、一般的な話まで広げると、デモ用とか本番用とかで含めたいシーンやビルド設定が異なる、とかあれば同じようにスクリプトからビルドがしたいなーというケースがあるのかもしれません。
とりあえず、そういう状況ということもあり、ビルドスクリプトを用意することにしました。
メニューからボタン一発でビルドできるようにします!
ビルドスクリプト(WebGL)
using UnityEngine;
using UnityEditor;
using System.IO;
using Debug = UnityEngine.Debug;
public class WebGLBuildScript
{
// 出力先のビルドパス(適宜変更してください)
private static readonly string BUILD_PATH = "Build/WebGL/YourProject";
// エクスプローラーでフォルダを開く
private static void OpenInExplorer(string path)
{
string normalizedPath = Path.GetFullPath(path);
if (!Directory.Exists(normalizedPath))
{
Debug.LogError($"指定されたパスが存在しません: {normalizedPath}");
return;
}
try
{
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = "explorer.exe",
Arguments = $"\"{normalizedPath}\"",
UseShellExecute = true
};
Process.Start(startInfo)?.Dispose();
}
catch (System.Exception e)
{
Debug.LogError($"フォルダを開く際にエラー: {e.Message}");
}
}
[MenuItem("Build/WebGL/Build Project")]
public static void BuildProject()
{
// 現在の設定を退避
var prevProductName = PlayerSettings.productName;
var prevVersion = PlayerSettings.bundleVersion;
var prevWidth = PlayerSettings.defaultWebScreenWidth;
var prevHeight = PlayerSettings.defaultWebScreenHeight;
try
{
// 一時的な設定(任意で変更)
PlayerSettings.productName = "Sample WebGL Game";
PlayerSettings.bundleVersion = "1.0.0";
PlayerSettings.defaultWebScreenWidth = 800;
PlayerSettings.defaultWebScreenHeight = 600;
// ビルドターゲットをWebGLに設定
EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTargetGroup.WebGL, BuildTarget.WebGL);
// ビルドするシーンを指定(プロジェクトに合わせて調整)
string[] scenes = new string[]
{
"Assets/Scenes/Main.unity"
};
foreach (var scene in scenes)
{
if (!File.Exists(scene))
throw new FileNotFoundException($"シーンが見つかりません: {scene}");
}
// 出力先を作成
string fullPath = Path.GetFullPath(BUILD_PATH);
Directory.CreateDirectory(fullPath);
// ビルドオプション
BuildOptions options = BuildOptions.None;
options |= BuildOptions.AutoRunPlayer;
// ビルド実行
var report = BuildPipeline.BuildPlayer(scenes, fullPath, BuildTarget.WebGL, options);
if (report.summary.result == UnityEditor.Build.Reporting.BuildResult.Succeeded)
{
Debug.Log($"ビルド成功: {fullPath}");
OpenInExplorer(fullPath);
}
else
{
Debug.LogError("ビルドに失敗しました。");
}
}
catch (System.Exception ex)
{
Debug.LogError($"ビルドエラー: {ex.Message}");
}
finally
{
// 設定を元に戻す
PlayerSettings.productName = prevProductName;
PlayerSettings.bundleVersion = prevVersion;
PlayerSettings.defaultWebScreenWidth = prevWidth;
PlayerSettings.defaultWebScreenHeight = prevHeight;
}
}
}
実際に使ったものから個人的な情報とかを外したシンプルバージョンにしたので、状況に合わせて修正して使ってみてください。
修正ポイント
PlayerSettings.productName
などの設定を一時的に上書き- ビルド対象のシーンも明示的に変更
- 出力先のフォルダをパターンごとに分離
- ビルド完了後にフォルダを自動で開く
- メニューから複数バージョンを選んでビルドできるように(Menu設定部分を増やしたらOK)
例えばこんな形で複数のビルド関数+Menu設定を用意すれば…
BuildDemoVersion()
BuildProductionVersion()
BuildEventVersion()
メニューからそれぞれを選んで、必要なときにワンクリックでビルドできます。
最後に
正直個人レベルの開発でCIとか使うわけでも無いと思うので、必要か?と言われれば微妙ですが、
私の使い方だとあった方が良いレベルだったので用意してみました。
もし興味がある方がいたらぜひ使ってみてください!(iOS, Androidなどもいけますよ)
コメント