うにてぃブログ

主にUnityとC#に関する記事を書いていきます

【Unity】Addressable をローカルサーバを立ててそこからロードする

Hosting Window から Hosting するように変更

ブラウザから記述してあるURLにアクセスし、 Console にログが表示されれば起動しています

今回であれば http://192.168.1.8:59595/ が URL になります

これを先にしないと以下のように[HostingServicePort]が置き換わらずエラーとなってしまう

System.Exception: Unable to load asset of type System.Object from location http://192.168.1.8:HostingServicePort/hoge.bundle.

リモートからDLする予定のグループのPaths を Remote に変更する

Include in Build にチェックを入れる

Remote のカタログを生成するために、Build Remote Catalog にチェックを入れる

アセットをビルドする

この際に特に設定をいじってなければ ServerData/[BuildTarget] のパスに生成される

Remote から読み込むように Use Existing Build に変更する

あとは適当にリモートとローカルから読み込むようなスクリプトを実行すれば

using System.Collections;
using UnityEngine;
using UnityEngine.AddressableAssets;
using Object = UnityEngine.Object;

public class SampleMonoBehaviour : MonoBehaviour
{
    public string RemotePath;
    public string LocalPath;

    private IEnumerator Start()
    {
        yield return Addressables.InitializeAsync();
        
        var local = Addressables.LoadAssetAsync<GameObject>(LocalPath);
        var remote = Addressables.LoadAssetAsync<GameObject>(RemotePath);
        
        yield return local;
        yield return remote;

        var localObject = Object.Instantiate(local.Result);
        var remoteObject = Object.Instantiate(remote.Result);
        localObject.transform.position = Vector3.down;
        remoteObject.transform.position = Vector3.up;

        yield return new WaitForSeconds(2);
        
        Object.Destroy(localObject);
        Object.Destroy(remoteObject);
        
        Addressables.Release(local);
        Addressables.Release(remote);
    }
}

ちゃんとインスタンスが生成されることが確認できる