facebook twitter hatena line email

Unity/Addressable

提供: 初心者エンジニアの簡易メモ
2022年9月7日 (水) 15:20時点におけるAdmin (トーク | 投稿記録)による版 (Addressableの作り方)

移動: 案内検索

Addressableをscriptで実行

Unity/Editor/Addressableビルド [ショートカット]

Addressableとは

外部サーバーで使えるResources

インストール

  1. Unityメインメニュー/Window/Package Manager/Unity Registryが選択"addressable"を検索
  2. Addressablesをinstall

Addressableの作り方

  1. 例として、プレハブを作るのでAssetsの下にResourcesフォルダがなければ作る。
  2. 適当にプレハブをAssets/Resourcesの下につくる。(例:ButtonGroup)
  3. inspectorにaddressableのチェックがあるので、チェックを付ける
  4. チェックをつけると、addressableのpathが生成される(例:ButtonGroup)
  5. Resources_movedに勝手に移動される。もしくは、作られたファイルをResources以外のdirに入れる。
  6. 以下SampleScene.csをシーンに連携する
using System.Collections;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.UI;

public class SampleScene : MonoBehaviour
{
    [SerializeField]
    Button clearButton;
    [SerializeField]
    Button loadButton;
    [SerializeField]
    Button unloadButton;
    [SerializeField]
    GameObject canvas;
    GameObject instance;
    void Start()
    {
        clearButton.onClick.AddListener(() => {
            Caching.ClearCache();
        });
        loadButton.onClick.AddListener(() => {
            StartCoroutine(Load());
        });
        unloadButton.onClick.AddListener(() => {
            Destroy(instance);
            Addressables.ReleaseInstance(instance); // 破棄
        });
    }
    IEnumerator Load()
    {
        // プレハブ:GameObject、画像:Image、スプライト:Sprite
        var handle = Addressables.LoadAssetAsync<GameObject>("ButtonGroup");
        yield return handle;
        if (handle.Status == AsyncOperationStatus.Succeeded)
        {
            instance = Instantiate(handle.Result, canvas.transform);
        }
    }
}

プレビュー実行すると、シーンのCanvasの下に、Addressableで作った、プレハブが表示される。

"Exception encountered in operation CompletedOperation, status=Failed, result= : Exception of type"エラーが出るとき

Addressable化したもののパスが合ってるか確認、PlayModeScript(後述)が、サーバーから取得するようになってないか。

設定確認

  1. Unityメインメニュー/Window/AssetManagement/Addressable/Groups/Profile/ManageProfiles
BuildTarget:[UnityEditor.EditorUserBuildSettings.activeBuildTarget]
LocalBuildPath:[UnityEngine.AddressableAssets.Addressables.BuildPath]/[BuildTarget]
LocalLoadPath:{UnityEngine.AddressableAssets.Addressables.RuntimePath}/[BuildTarget]
RemoteBuildPath:ServerData/[BuildTarget]
RemoteLoadPath:http://localhost/[BuildTarget]

設定変更

設定変更したい場合は、

  1. Unityメインメニュー/Window/Addressable/Groups/ManageProfiles/Create/NewProfileを選択し、
  2. 各項目を入力していく

プレイモード選択

  1. Unityメインメニュー/Window/Addressable/Groups/PlayModeScriptを選択
  • Use Asset Database (fastest):Unity内のアセットから
  • Simulate Groups (Advanced):依存関係を分析して取得
  • Use Existing Build(requires built groups):パックプレイモードで、サーバーから取得

Addressableでビルドしたものを、Addressableを取得

  1. 上記のPlayModeScriptを"Use Existing Build"へ
  2. Addressableをビルドする(Unityメインメニュー/Window/Asset Management/Addressables/Groups/Build/New Build/Default Build Script
  3. プレビュー実行

ちなみにBuild時に以下のようなファイルが出来る。

  • Library/com.unity.addressables/aa/WebGL/AddressablesLink
  • Library/com.unity.addressables/aa/WebGL/catalog.json
  • Library/com.unity.addressables/aa/WebGL/settings.json
  • ServerData/WebGL/defaultlocalgroup_assets_all_69745f2bc65176429eb31f083306322b.bundle

Addressableの削除

  1. Unityメインメニュー/Window/Asset Management/Addressables/Groupsを選択
  2. 以下3種類削除操作がある
  • Build/CleanBuild/All → 以下2つのLibrary/com.unity.addressablesと、Library/BuildCacheが、削除される
  • Build/CleanBuild/"Content Builders"  → Library/com.unity.addressables が、削除される
  • Build/CleanBuild/"Build Pipeline Cache" → Library/BuildCache が、削除される

Addressableの再生成

  1. Unityメインメニュー/Window/Asset Management/Addressables/Groups
  2. Build/New Build/Default Build Script

プロジェクト以下/ServerData/WebGL/とかにできる

ServerData/WebGL/catalog_v1.0.hash
ServerData/WebGL/catalog_v1.0.json
ServerData/WebGL/localization-assets-shared_assets_all.bundle
ServerData/WebGL/localization-string-tables-chinese(traditional)(zh-tw)_assets_all.bundle
ServerData/WebGL/localization-string-tables-english(unitedstates)(en-us)_assets_all.bundle
ServerData/WebGL/localization-string-tables-japanese(japan)(ja-jp)_assets_all.bundle
Library/BuildCache/
Library/com.unity.addressables/aa/
Library/com.unity.addressables/WebGL/

ビルド時のパスを確認

  1. Assets/AddressableAssetsData/AssetGroups/Default Local Group
  2. BuildPathとLoadPathに何を設定してるか確認。
  • LocalBuildPath:Library/com.unity.addressables/aa/WebGL/WebGL
  • LocalLoadPath:{UnityEngine.AddressableAssets.Addressables.RuntimePath}/WebGL
  • RemoteBuildPath:ServerData/WebGL
  • RemoteLoadPath:ttp://localhost/WebGL

サーバーにAddressableファイルを読み込んで表示

  1. Assets/AddressableAssetsData/AssetGroups/Default Local Group
  2. BuildPathにRemoteBuildPathを設定し、
  3. LoadPathにRemoteLoadPathを設定
  4. Addressableをビルドし、(Unityメインメニュー/Window/Asset Management/Addressables/Groups/Build/New Build/Default Build Script
  5. ServerData/WebGLにできたファイルを自分のサーバー(例:ttp://localhost/WebGL/~)へ上げる。
  6. Unityメインメニュー/Window/Addressable/Groups/Profile/ManageProfilesのRemoteLoadPathをttp://localhost/[BuildTarget]から、自分のサーバー(例:ttp://localhost/WebGL/~)のファイルのある場所へ。
  7. プレイモード(Unityメインメニュー/Window/Addressable/Groups/PlayModeScript)をUse Existing Buildへ
  8. プレビュー表示すると表示される。

AddressablesのRemoteLoadPathの変更

  1. Window/Asset Management/Addressables/Groups
  2. Profile:WebGLを選択
  3. ManageProfiles
  4. RemoteLoadPathを変更する

https://qiita.com/tetr4lab/items/1b26755089820b041b4f

AddressableでダウンロードしたAssetBundleデータの削除

スクリプトでの削除

Caching.ClearCache();

Addressableでロードしたアプリ内で、実行してもキャッシュは消えなかった。利用中のキャッシュが消えないのかも。 一度アプリを消して、再度キャッシュクリア処理だけ実行したら消えた。

AddressableでダウンロードしたAssetBundleデータの保存先

macの場合の保存先

~/Library/Caches/Unity/[社名]_AddressablesProject/__info
~/Library/Caches/Unity/[社名]_AddressablesProject/e93a4ab8b320d6c828e139bbc64c9600/6311e87ae18bf58221e098afaa7d0f47/__data
~/Library/Caches/Unity/[社名]_AddressablesProject/e93a4ab8b320d6c828e139bbc64c9600/6311e87ae18bf58221e098afaa7d0f47/__info

windowsの場合の保存先

~\AppData\LocalLow\Unity\[CompanyName]_[ProductName]

androidの場合の保存先

/storage/emulated/0/Android/data/[PackageName]/files/

参考:https://yotiky.hatenablog.com/entry/unity_addresableassetsssytem

参考:https://baba-s.hatenablog.com/entry/2020/04/04/091500

StreamingAssetsディレクトリについて

Addressableをビルドして、UnityプロジェクトのWebGLをBuildすると、

  • Build
  • TemplateData
  • index.html

と同じ場所に、

  • StreamingAssets

ができる。

"Player content must be built before entering play mode with packed data."エラーが出る場合

詳細エラーメッセージ

Player content must be built before entering play mode with packed data.  This can be done from the Addressables window in the Build->Build Player Content menu command.
UnityEngine.GUIUtility:ProcessEvent

一度addressableをビルドする。

プロジェクトをビルドしたときのAddressableリンクデータ

プロジェクトビルドファイルの下に、"StreamingAssets"ができる。 これがないとAddressableにアクセスできない。

参考

https://robamemo.hatenablog.com/entry/2021/01/08/195415

https://light11.hatenadiary.com/entry/2019/12/26/225232

https://light11.hatenadiary.com/entry/2020/07/29/202755