facebook twitter hatena line email

「Unity/Addressable/複数ロード」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(サンプル)
(サンプル)
行46: 行46:
  
 
実行に失敗する場合は、Addressablesを再ビルドしてるか確認する
 
実行に失敗する場合は、Addressablesを再ビルドしてるか確認する
 +
 +
参考:https://robamemo.hatenablog.com/entry/2021/01/09/102629

2022年10月10日 (月) 16:04時点における版

準備

  1. Unityメインメニュー/Window/Asset Management/Addressables/Groupsを選択
  2. ロードしたいオブジェクトのlabels部分にラベル名を追加(例:"TestLabel")

サンプル

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.UI;

public class AssetLabelsLoadScene : MonoBehaviour
{
    [SerializeField]
    Button loadButton;
    [SerializeField]
    GameObject canvas;
    GameObject instance;
    void Start()
    {
        loadButton.onClick.AddListener(() => {
            if (instance == null)
            {
                StartCoroutine(Load());
            }
        });
    }
    IList<GameObject> list;
    IEnumerator Load()
    {
        var handle = Addressables.LoadAssetsAsync<GameObject>("TestLabel", null);
        yield return handle;
        if (handle.Status == AsyncOperationStatus.Succeeded)
        {
            list = handle.Result;
            foreach (var value in list)
            {
                Debug.Log(value.name);
            }
        }
    }
}

実行に失敗する場合は、Addressablesを再ビルドしてるか確認する

参考:https://robamemo.hatenablog.com/entry/2021/01/09/102629