6
5

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 1 year has passed since last update.

[Unity]AddressableAssetSystemでゲームステージのPrefabをダウンロードする

Last updated at Posted at 2023-03-19

概要

AddressableAssetSystemでゲームステージのPrefabをサーバーからダウンロードする流れをまとめます。
詳しい技術的な解説は載せませんが、とりあえず一回手を動かして動作確認してみたい!という方用の記事です。

本文

環境の準備

3D(URP)で新規プロジェクトを作って
スクリーンショット 2023-03-19 17.12.48.png

PackageManagerでAddressablesをインストールします。
スクリーンショット 2023-03-19 18.04.11.png

今回はゲームステージをダウンロードするテストなのでスクショのようなPrefabを用意しました。テストするだけなら単純にキューブひとつだけでも大丈夫です。
スクリーンショット 2023-03-19 17.14.50.png

Addressableの準備

Window > Asset Management > Addressables > GroupでAddressables Groupsを開きます。
ウィンドウの中に「Create Addressables Settings」というボタンがあるのでそれを押して開始します。
スクリーンショット 2023-03-19 17.32.47.png

New > Packed Assetsでグループを新規作成します。生成されたグループ(Packed Assets)を右クリック > Renameで適当なグループ名に変更してください。(今回はRemoteGroupにしました)
スクリーンショット 2023-03-19 17.34.59.png

PrefabをRemoteGroupにドラッグ&ドロップするとPrefabがAddressableAssetの管理対象に登録されます。(Scene上のゲームオブジェクト類じゃない点にご注意ください!)
スクリーンショット 2023-03-19 17.39.51.png

続いてRemoteGroupを選択してInspectorを確認します。
Build & Load Pathsを<custom>に変更
Build PathをRemote.BuildPathに変更
LoadPathをRemote.LoadPathに変更します。
スクリーンショット 2023-03-19 17.47.45.png

Play Mode Script > Use Existing Buildを選択しておきます
スクリーンショット 2023-03-19 21.50.35.png

ProjectウィンドウからAssets/AddressableAssetsData/AddressableAssetSettings.assetを選択してInspectorを確認します。
Player Version Overrideに適当な文字列を指定します(今回は1にしました)
Build Remote Catalogにチェックを入れます。
スクリーンショット 2023-03-19 17.57.11.png

次にメニューからWindow > Asset Management > Addressables > Profilesを選択します。
スクリーンショット 2023-03-19 21.39.53.png

Addressables Profilesウィンドウが表示されますのでRemoteはCustomを選んでRemote.LoadPathにデータをアップロードする予定のサーバーのURLを入力します。アップロードするファイルの親になるフォルダまでを指定したURLになります(URLがまだ決まってなければ後で設定しても大丈夫です)
スクリーンショット 2023-03-19 21.41.03.png

ビルド

Addressables GroupsでBuild > New Build > Default Build Scriptを選択するとビルドが開始されます。
スクリーンショット 2023-03-19 18.20.02.png

設定を色々いじってるとAddressableが思ったように動いてくれなくなる時があります。原因がわからなくて困ったときは
Addressables GroupsでBuild > Clean Build > Allを一度して、再度Build > New Build > Default Build Scriptをすると上手くいく事もあります。
スクリーンショット 2023-03-19 21.35.01.png

ビルドで作成されたファイルはここまでの設定の中にあるBuild Pathで指定されたフォルダの中に作成されます。Finderやエクスプローラでプロジェクトのフォルダから今回の例では「ServerData/StandaloneOSX」を辿るとファイルがあります。フォルダの中のファイルをFTPでサーバーにアップロードします。(サーバー上ファイルのパーミッションは644(読み込み専用ファイル)になっていれば正常に動作できると思います)
スクリーンショット 2023-03-19 21.28.29.png
これでAddressableの下準備ができました。

動作確認

以下のスクリプトを作成してHierarchy上に作成したオブジェクトに貼り付けてください。(AssetReferenceGameObjectにはゲームステージPrefabをドラッグ&ドロップします)

Projectウィンドウ上のPrefab、Addressables Groups上のPrefabどちらでも大丈夫です。

動作確認のためゲームステージPrefabはHierarchy上から消しておきます(実行に合わせてゲームステージが表示されたら成功です)

スクリーンショット 2023-03-19 21.59.12.png


using UnityEngine;
using UnityEngine.AddressableAssets;

public class AddressableSample : MonoBehaviour
{
    [SerializeField] private AssetReferenceGameObject assetReferenceGameObject;

    private GameObject _spawnedGameObject;
    
    private void Start()
    {
        assetReferenceGameObject.InstantiateAsync().Completed +=
            (asyncOperation) => _spawnedGameObject = asyncOperation.Result;
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            // インスタンスをリリースする
            assetReferenceGameObject.ReleaseInstance(_spawnedGameObject);
        }
    }
}

実行するとサーバーからダウンロードされたゲームステージが表示されました。成功です!
スペースキーを押すとインスタンスもしっかりリリースされることが確認できると思います。
スクリーンショット 2023-03-19 22.01.59.png

もっと詳しく動作確認したいときは

ProjectウィンドウからAssets/AddressableAssetsData/AddressableAssetSettings.assetを選択してInspectorを確認します。
Send Profiler Eventsにチェックを入れます。
スクリーンショット 2023-03-20 8.06.33.png

メニューからWindow > Asset Management > Addressables > Event Viewerを選択すると
スクリーンショット 2023-03-20 8.03.19.png

Addressables Event Viewerが表示されます。この画面でメモリの使用状況などを確認することができます。
スクリーンショット 2023-03-20 8.18.44.png

6
5
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
6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?