Unity/Addressable/画像ロード
提供: 初心者エンジニアの簡易メモ
手順
- Assets/Sprites/Icon.pngを用意
- Icon.pngのInspectorを開いてAddressableにチェックをいれ、Addressableのパスを"Assets/Sprites/Icon.png"で設定
サンプル
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AddressableAssets; using UnityEngine.ResourceManagement.AsyncOperations; using UnityEngine.UI; public class SpriteLoadScene : MonoBehaviour { [SerializeField] Button clearButton; [SerializeField] Button loadButton; [SerializeField] Button unloadButton; [SerializeField] GameObject canvas; [SerializeField] Image image; AsyncOperationHandle<Sprite> spriteHandle; void Start() { clearButton.onClick.AddListener(() => { Caching.ClearCache(); }); loadButton.onClick.AddListener(() => { StartCoroutine(Load()); }); unloadButton.onClick.AddListener(() => { Addressables.Release(spriteHandle); // 破棄 }); } IEnumerator Load() { var handle = Addressables.LoadAssetAsync<Sprite>("Assets/Sprites/Icon.png"); yield return handle; if (handle.Status == AsyncOperationStatus.Succeeded) { spriteHandle = handle; image.sprite = handle.Result; } } }