はじめに
UnityでWebGL開発を行っているときにビルド後デプロイしたい時があります。OSSとして公開しているプロジェクトの場合、デプロイ先としてGitHub Pageを選んだとしてもアセットをpublicにすることができません。 今回は、ローカルでビルド後 WebGLデプロイ用のブランチに自動で pushする機能を作りました。
成果物
WebGL Deploy URL
プロジェクトリポジトリ
サンプルプロジェクトの成果物として、InputSystemをいろいろ使っているプロジェクトです
動作環境
- Window10
- Unity 2021.3.3f1
- Git,GitHubが使えるPC
実装
Build後に特定の処理を行う
ビルド処理が終わった後に処理を挟みたい場合は、IPostprocessBuildWithReport
を継承したスクリプトを使用します。
2019.1以前は IPostprocessBuild
がありましたが、こちらは現時点では非推奨になっています。
docs.unity3d.com
githubへのcommit/pushは Deploy.bat
にまとめています。
外部のファイルを実行する際に process.StartInfo.FileName
に実行するファイル名を設定します。
また process.StartInfo.CreateNoWindow
を trueにすることで、cmdが立ち上がらずにバックグラウンド処理になります。
public class WebGLDeploy : IPostprocessBuildWithReport { public int callbackOrder { get; } private string _batPath; public void OnPostprocessBuild(BuildReport report) { _batPath = Path.Combine(GetBuildFolderPath(), "Build"); RunCommand(_batPath); } private static string GetBuildFolderPath() { return Directory.GetParent(Application.dataPath)?.FullName; } private static void RunCommand(string buildFolderPath) { var process = new Process(); process.StartInfo.WorkingDirectory = buildFolderPath; process.StartInfo.FileName = Path.Combine(buildFolderPath, "Deploy.bat"); process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardInput = false; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.CreateNoWindow = true; process.Start(); process.WaitForExit(); } }
BuildデータをGitHub に スクリプト上から Commit/Pushする
事前に Deploy.bat
を Buildフォルダに入れておきます。
Batファイルは、以下のように定義しています。
git add * git commit -m "Deploy" git push origin webgl_build