3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

GitHub ActionsからUnityに値(パラメーター)を渡したり、UnityからGitHub Actionsへログ表示したりエラー終了させたりする

Last updated at Posted at 2021-05-30

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上のログでは下記のように表示されます。

スクリーンショット 2021-05-31 0.34.20.png

スクリーンショット 2021-05-31 0.12.52.png

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?