Version: 2023.1
言語: 日本語
カスタムアセットパックの作成
Android のグラフィックス

ランタイムのアセッパックの管理

Unity provides APIs to manage asset packs at runtime. They use Google’s PlayCore API, which means they have the same limitations as PlayCore, and can’t manage install-time asset packs. Using the PlayCore API also means your application requires the PlayCore plug-in. If your project has asset packs, either custom asset packs or Unity-generated asset packs, Unity automatically adds the PlayCore dependency to the application’s manifest.

アセットパックをダウンロードし、そのアセットにアクセスする方法は、アセットパックの配信モードによって異なります。アセットパックの配信モードは 3 種類あります。

  • install-time: Google Play は、デバイスがアプリケーションをインストールする際に install-time アセットパックを自動的にダウンロードします。Google Play では、これらのアセットパックを基本アプリケーションの一部とみなし、エンドユーザーはアプリケーション全体をアンインストールしない限り、アセットパックをアンインストールすることはできません。PlayCore API は、 install-time アセットパックを処理しません。つまり、 install-time アセットパックの状態を確認したり、ダウンロードを要求したり、削除したりすることはできません。また、これらのアセットパック内のアセットに直接アクセスすることもできません。ただし、Unity が生成した install-time アセットパック内のストリーミングアセットは例外です。ストリーミングアセットにアクセスするには、Application.streamingAssetsPath を使用してストリーミングアセットの場所へのパスを取得し、UnityWebRequest を使用してそのパス内のアセットにアクセスします。カスタムアセットパックを作成した場合、標準のファイル API を使ってその中のアセットにアクセスすることはできません。代わりに、Android の AssetManager API を使用します。
  • fast-follow: Google Play automatically starts to download fast-follow asset packs after it installs the application. However, it’s possible that not all fast-follow asset packs are available on the first time the application launches. To check the status and download fast-follow asset packs, refer to Download asset packs.
  • on-demand: Google Play doesn’t automatically download on-demand asset packs. You have to manually start the download. For information on how to do this, refer to Download asset packs.

For more information about delivery modes, refer to Delivery modes.

アセットパックのダウンロード

If your application uses fast-follow or on-demand asset packs, the device must download these asset packs before the application can access assets inside of them. To check the status of asset packs and download them if they’re not on the device, you must first know the name of each asset pack. To get the names of Unity-generated asset packs, call AndroidAssetPacks.GetCoreUnityAssetPackNames. There is no runtime API to get the names of custom asset packs so you must keep track of them yourself. You set the name of custom asset packs at build time; it’s the name of the directory.

After you have the names of your asset packs, to check the status of each asset pack, call AndroidAssetPacks.GetAssetPackStateAsync, passing in the asset pack name. This returns the status of the asset pack you query, and you can use the result to determine whether you need to download the asset pack. If you want to quickly query the status of every Unity-generated asset pack, you can use AndroidAssetPacks.coreUnityAssetPacksDownloaded. This helps you to ensure that every Unity-generated asset pack is available before you load any scene other than the first one or try to access other resources that Unity handles.

ダウンロードが必要なアセットパックごとに、AndroidAssetPacks.DownloadAssetPackAsync にアセットパックの名前を渡して呼び出します。アセットパックのダウンロード中は、ダウンロード状態を監視してください。なぜなら、ダウンロードが一時停止したり、失敗したりすることがあるからです。これを行うには 2 つの方法があります。

  • AndroidAssetPacks.DownloadAssetPackAsync が返すDownloadAssetPackAsyncOperation インスタンスを定期的に確認してください。
  • 2 番目のパラメーターとしてコールバックを受け取る AndroidAssetPacks.DownloadAssetPackAsync のバージョンを使用してください。渡すコールバックは、ダウンロードの状態を判断するために使用する AndroidAssetPackInfo をパラメーターとしてとる必要があります。

その他の参考資料

カスタムアセットパックの作成
Android のグラフィックス