「Unity/SpriteAtlas/Addressable」の版間の差分
提供: 初心者エンジニアの簡易メモ
(ページの作成:「==SpriteAtlasファイルの設定== AllowRotationとTightPackingのチェックを外す。 ==SpriteAtlasファイルをAddressableへ== #SpriteAtlasファイルのAddr...」) |
(→サンプルコード) |
||
| (同じ利用者による、間の2版が非表示) | |||
| 行4: | 行4: | ||
==SpriteAtlasファイルをAddressableへ== | ==SpriteAtlasファイルをAddressableへ== | ||
#SpriteAtlasファイルのAddressableにチェックを入れて、Addressableのパスを追加する | #SpriteAtlasファイルのAddressableにチェックを入れて、Addressableのパスを追加する | ||
| − | # | + | #SpriteAtlasファイルのIncludeInBuildを外す。(AddressableがRemote設定の場合のみ) |
Addressableのパス例 | Addressableのパス例 | ||
| − | Assets/SpriteAtlas/IconSpriteAtlas.spriteatlas | + | Assets/SpriteAtlas/IconSpriteAtlas.spriteatlas |
| + | 画像名例 | ||
| + | favorite_18px | ||
| + | |||
| + | ===サンプルコード=== | ||
| + | SpriteAtlasの画像内のアイコンをAddressableで取ってきて、Imageに貼り付けるサンプルコード | ||
| + | <pre> | ||
| + | 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; | ||
| + | } | ||
| + | } | ||
| + | </pre> | ||
==参考== | ==参考== | ||
https://kondeneenen.com/spriteatlas_for_aas/ | https://kondeneenen.com/spriteatlas_for_aas/ | ||
2024年11月12日 (火) 10:29時点における最新版
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;
}
}
