LoginSignup
0
1

More than 1 year has passed since last update.

UnityでAssetPackのビルドをビルドパイプラインに組み込む

Last updated at Posted at 2021-06-09

UnityでPlay Asset Deliveryを使うアプリのビルドを、Googleが提供するビルド方法(Unityエディタの専用メニューからビルド)ではなく、一般的なビルドパイプラインから呼び出す方法をまとめました。
これで BuildSettingsウィンドウのビルドや、Unity Cloud Buildによるビルドでも、AppBundleにAssetPackが含まれるようになります。

Google Play Pluginに含まれるPlay Asset Delivery Unity APIというライブラリを使用しますが、一部コードを書き換えます。
お行儀が悪いですが、Unityエディタの専用メニューからビルドするようにしか作らないGoogleさんが悪い。嘘です、ライブラリがあるだけでもとても助かります。

検証環境

  • Unity 2019.4.22f1
  • Google Play Plugin ver 1.4.0

コード

AppBundleBuilderを修正する

1. AndroidPlayerFilePathをpublicにする

Before

AppBundleBuilder.cs
private string AndroidPlayerFilePath
{
    get { return Path.Combine(_workingDirectoryPath, AndroidPlayerFileName); }
}

After

AppBundleBuilder.cs
public string AndroidPlayerFilePath
{
    get { return Path.Combine(_workingDirectoryPath, AndroidPlayerFileName); }
}
2. BuildAndroidPlayerメソッドの冒頭にあるディレクトリ作成処理をメソッドに切り出す

Before

AppBundleBuilder.cs
public virtual bool BuildAndroidPlayer(BuildPlayerOptions buildPlayerOptions)
{
    var workingDirectory = new DirectoryInfo(_workingDirectoryPath);
    if (workingDirectory.Exists)
    {
        workingDirectory.Delete(true);
    }

    workingDirectory.Create();

       :
       :


After

AppBundlePublisher.cs

public void EnsureWorkingDirectory()
{
    var workingDirectory = new DirectoryInfo(_workingDirectoryPath);
    if (workingDirectory.Exists)
    {
        workingDirectory.Delete(true);
    }

    workingDirectory.Create();

}

public virtual bool BuildAndroidPlayer(BuildPlayerOptions buildPlayerOptions)
{
    EnsureWorkingDirectory();

AppBundlePublisher.csを修正する

AppBundlePublisherの適当な箇所にメソッドを追加します。
もしかしたらSystem.IOあたりのusingを追加する必要もあるかもしれません。

AppBundlePublisher.cs

public static void PackAsset(string aabFilePath, AssetPackConfig config)
{
    var appBundleBuilder = CreateAppBundleBuilder();
    if (!appBundleBuilder.Initialize(new BuildToolLogger()))
    {
        return;
    }

    appBundleBuilder.EnsureWorkingDirectory();
    File.Copy(aabFilePath, appBundleBuilder.AndroidPlayerFilePath, true);

    Debug.LogFormat("Building app bundle: {0}", aabFilePath);
    appBundleBuilder.CreateBundle(aabFilePath, config);
}

ポストプロセスビルド処理を追加する

適当なクラス名でIPostprocessBuildWithReportを実装します。

public class PublishPlayAssetDelivery : IPostprocessBuildWithReport
{
    public int callbackOrder => 0;

    public void OnPostprocessBuild(BuildReport report)
    {
        if (report.summary.platform != BuildTarget.Android)
        {
            return;
        }

        var config = AssetPackConfigSerializer.LoadConfig();
        AppBundlePublisher.PackAsset(report.summary.outputPath, config);
    }
}

最後に

Googleさんが提供している専用のビルドメニューでは下記のような動作をしています。

  1. AppBundleをビルドしてテンポラリディレクトリに出力する
  2. AssetPackをビルドする
  3. AppBundleを解凍して、必要なAssetPackを組み込む
  4. 再度パッキングして、本来の出力先に保存する

上記の手続きが一連の流れとしてコーディングされていて、自分にとって都合が悪かったため、今回は3と4を抜き出してみました。

0
1
1

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
0
1