GitHub ActionsからUnityに値(パラメーター)を渡す
対象になるGitHub Action
Game CIのBuilderを使ってビルドしているプロジェクト向けです。
準備 (コードをプロジェクトに配置)
下記コードをAssets/Editor/ArgumentsParser.cs
に設置
[GitHub Actions] パラメーターの設定
ビルド用ymlにcustomParameters
を設定します。
記述ルール : -パラメーター 値
例) -myCustomParameter CustomParam
複数定義も可能です。
例) -fruits1 apple -fruits2 orange
値を設定しない場合はboolタイプのtrueと判断されます
例) -enableMyAction
※ 半角スペースで区切る為、パラメーター/値に半角スペースは使えません。
build.yml
# 省略
- uses: game-ci/unity-builder@v2
env:
UNITY_LICENSE: ${{ secrets.UNITY_LICENSE }}
with:
customParameters: -myCustomParameter CustomParam ← 追加
# 省略
[Unity] パラメーターの取得
ビルドメソッドからArgumentsParser.GetValidatedOptions()
を呼ぶとパラメーターと値が格納された連想配列が取得できます。そこからパラメーター名で取得します。
Build.cs
// 省略
var options = ArgumentsParser.GetValidatedOptions();
var param = options["myCustomParameter"];
Console.WriteLine(param);
// GitHub ActionsのログにCustomParamと表示される
// 省略
[余談] Game CIのパラメーターの取得
Game CIが利用するパラメーターを取得する事もできます。
// 省略
var options = ArgumentsParser.GetValidatedOptions();
options["projectPath"]
options["buildTarget"]
options["customBuildPath"]
options["buildVersion"]
// 省略
UnityからGitHub Actionsへログ出力したりエラー終了させたりする
メソッドの定義
下記のようなメソッドを生やしておくと便利です。
UnityとGitHub兼用のログを表示するメソッドです。
エラー表示時はビルドを強制終了させるようにしています。
public static void PrintLog(string msg)
{
if (Application.isBatchMode) Console.WriteLine(msg);
else Debug.Log(msg);
}
public static void PrintWarningLog(string msg)
{
if (Application.isBatchMode) Console.WriteLine($"::warning:: {msg}");
else Debug.LogWarning(msg);
}
public static void PrintErrorLog(string msg)
{
if (Application.isBatchMode)
{
Console.WriteLine($"::error:: {msg}");
// ビルドの強制終了 100は適当です。 1以上を指定します。0だと正常に完了したとみなされるので注意してください。
EditorApplication.Exit(100);
}
else Debug.LogError(msg);
}
使い方
PrintLog("メッセージ");
PrintWarningLog("warningだよ");
PrintErrorLog("Errorだよ");
GitHub Actions上のログでは下記のように表示されます。