facebook twitter hatena line email

Unity/Addressable/LoadResourceLocationsAsync

提供: 初心者エンジニアの簡易メモ
2025年7月17日 (木) 03:46時点におけるAdmin (トーク | 投稿記録)による版 (ページの作成:「==LoadResourceLocationsAsyncを使って複数ロード== <pre> using UnityEngine; using UnityEngine.UI; using System.Collections; using UnityEngine.AddressableAssets; us...」)

(差分) ←前の版 | 最新版 (差分) | 次の版→ (差分)
移動: 案内検索

LoadResourceLocationsAsyncを使って複数ロード

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

public class AssetLoadAllScene : MonoBehaviour
{
    AsyncOperationHandle<IList<IResourceLocation>> locationsHandle;
    [SerializeField]
    Button clearButton;
    [SerializeField]
    Button loadButton;
    [SerializeField]
    Button unloadButton;
    [SerializeField]
    Button instantiateButton;
    [SerializeField]
    Button destroyButton;
    [SerializeField]
    GameObject canvas;
    List<GameObject> loadedAssets;
    List<GameObject> instances;

    void Start()
    {
        loadedAssets = new List<GameObject>();
        instances = new List<GameObject>();
        clearButton.onClick.AddListener(() =>
        {
            Caching.ClearCache();
        });
        loadButton.onClick.AddListener(() =>
        {
            StartCoroutine(Load());
        });
        unloadButton.onClick.AddListener(() =>
        {
            if (locationsHandle.IsValid())
            {
                Addressables.Release(locationsHandle);
            }
            Debug.Log("unload!!");
        });
        instantiateButton.onClick.AddListener(() =>
        {
            InstantiateAsset();
        });
        destroyButton.onClick.AddListener(() =>
        {
            foreach (GameObject instance in instances)
            {
                Destroy(instance);
            }
        });
    }
    IEnumerator Load()
    {
        loadedAssets = new List<GameObject>();
        Debug.Log("Load!!");
        var keys = new List<string> { "SomeLabel", "Some2Label" };
        // var keys = "SomeLabel";
        locationsHandle = Addressables.LoadResourceLocationsAsync(keys, Addressables.MergeMode.Union);

        if (!locationsHandle.IsDone)
        {
            yield return locationsHandle;
        }
        var locations = locationsHandle.Result;

        foreach (var location in locations)
        {
            Debug.Log("location.PrimaryKey=" + location.PrimaryKey);
            StartCoroutine(LoadAssetAsync(location));
        }
        Debug.Log("Load end!!");
    }
    IEnumerator LoadAssetAsync(IResourceLocation location)
    {
        var handle = Addressables.LoadAssetAsync<GameObject>(location);
        yield return handle;

        if (handle.Status == AsyncOperationStatus.Succeeded)
        {
            loadedAssets.Add(handle.Result);
        }
    }
    void InstantiateAsset()
    {
        foreach (GameObject loadedAsset in loadedAssets)
        {
            GameObject instance = Instantiate(loadedAsset, canvas.transform);
            instance.transform.localPosition = new Vector2(Random.Range(-100f, 100f), Random.Range(-100f, 100f));
            instances.Add(instance);
        }
    }
}

参考: https://light11.hatenadiary.com/entry/2023/05/25/194333