Unity/SpriteAtlas/Addressable
提供: 初心者エンジニアの簡易メモ
SpriteAtlasファイルの設定
AllowRotationとTightPackingのチェックを外す。
SpriteAtlasファイルをAddressableへ
- SpriteAtlasファイルのAddressableにチェックを入れて、Addressableのパスを追加する
- SpriteAtlasファイルのIncludeInBuildを外す。(AddressableがRemote設定の場合のみ)
Addressableのパス例
Assets/SpriteAtlas/IconSpriteAtlas.spriteatlas
画像名例
favorite_18px
サンプルコード
SpriteAtlasの画像内のアイコンをAddressableで取ってきて、Imageに貼り付けるサンプルコード
using System.Collections;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.U2D;
using UnityEngine.UI;
public class SpriteAtlasScene : MonoBehaviour
{
AsyncOperationHandle<SpriteAtlas> handle;
[SerializeField] Image image;
IEnumerator Start()
{
yield return StartCoroutine(LoadSpriteAtlasIcon());
yield return StartCoroutine(BindSprite());
}
IEnumerator LoadSpriteAtlasIcon()
{
handle = Addressables.LoadAssetAsync<SpriteAtlas>("Assets/SpriteAtlas/IconSpriteAtlas.spriteatlas");
//await handle.Task;
yield return handle;
if (handle.Status == AsyncOperationStatus.Succeeded)
{
Debug.Log("success");
// atlasAction.Invoke(handle.Result);
}
}
IEnumerator BindSprite()
{
SpriteAtlas spriteAtlas = handle.Result;
Debug.Log("spriteAtlas.name=" + spriteAtlas.name);
if (spriteAtlas.name == "IconSpriteAtlas")
{
Sprite sprite = spriteAtlas.GetSprite("favorite_18px");
image.sprite = sprite;
}
yield return null;
}
}
