Addressable에 SpriteAtlas를 등록해서 Sprite를 받아오는 방식으로 로직 변경

This commit is contained in:
NTG_Lenovo 2025-07-16 13:33:47 +09:00
parent bcba4f0436
commit 12ff1ab2b9
5 changed files with 52 additions and 30 deletions

View File

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
#if UNITY_EDITOR
@ -61,6 +62,18 @@ public static async Task<T> LoadAsset<T>(AssetReference reference) where T : Uni
return null;
}
public static async Task<List<T>> LoadAssetsByLabel<T>(string label) where T : UnityEngine.Object
{
var handle = Addressables.LoadAssetsAsync<T>(label, null);
await handle.Task;
if (handle.Status == AsyncOperationStatus.Succeeded)
return handle.Result.ToList();
Debug.LogError($"[AssetManager] Failed to load assets with label: {label}");
return new List<T>();
}
public static async Task<SceneInstance> LoadScene(string key, LoadSceneMode mode = LoadSceneMode.Additive)
{
var handle = Addressables.LoadSceneAsync(key, mode);

View File

@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.U2D;
namespace DDD
{
@ -9,6 +11,8 @@ public class DataManager : Singleton<DataManager>, IManager
public ItemDataSo ItemDataSo { get; private set; }
public FoodDataSo FoodDataSo { get; private set; }
public EnvironmentDataSo EnvironmentDataSo { get; private set; }
private Dictionary<string, Sprite> _spriteAtlas;
public bool IsInitialized { get; private set; }
@ -24,6 +28,28 @@ public async void PostInit()
ItemDataSo = await AssetManager.LoadAsset<ItemDataSo>(DataConstants.ItemDataSo);
FoodDataSo = await AssetManager.LoadAsset<FoodDataSo>(DataConstants.FoodDataSo);
EnvironmentDataSo = await AssetManager.LoadAsset<EnvironmentDataSo>(DataConstants.EnvironmentDataSo);
List<SpriteAtlas> spriteAtlases = await AssetManager.LoadAssetsByLabel<SpriteAtlas>(DataConstants.AtlasLabel);
_spriteAtlas = new Dictionary<string, Sprite>(spriteAtlases.Count);
foreach (var atlas in spriteAtlases)
{
if (atlas == null) continue;
var count = atlas.spriteCount;
if (count == 0) continue;
var sprites = new Sprite[count];
atlas.GetSprites(sprites);
foreach (var sprite in sprites)
{
if (sprite == null) continue;
var key = sprite.name.Replace("(Clone)", "").Trim();
_spriteAtlas.TryAdd(key, sprite);
}
}
IsInitialized = true;
}
@ -40,6 +66,8 @@ public async Task WaitUntilInitialized()
await Task.Yield();
}
}
public Sprite GetSprite(string key) => _spriteAtlas.GetValueOrDefault(key);
// TODO : So가 늘어나는 경우 관리 방법 변경 필요성이 있음
// GetItemType(id)

View File

@ -15,6 +15,7 @@
using UnityEditor;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.U2D;
using ColorUtility = UnityEngine.ColorUtility;
public class GoogleSheetManager : Singleton<GoogleSheetManager>
@ -682,7 +683,6 @@ private async Task<bool> InternalCreateGoogleSheetSoAsync()
FieldInfo field = dataType.GetField(fieldName,
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
PropertyInfo property = dataType.GetProperty(fieldName,
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
@ -696,31 +696,7 @@ private async Task<bool> InternalCreateGoogleSheetSoAsync()
{
object value;
// Sprite, Color 등 기존 로직 유지
if (explicitType == "Sprite" && (field?.FieldType == typeof(Sprite) ||
property?.PropertyType == typeof(Sprite)))
{
string spriteKey = prop.Value.ToString().Trim();
if (string.IsNullOrEmpty(spriteKey)) continue;
if (!AssetManager.HasLabel(spriteKey, "Sprite"))
{
Debug.LogWarning($"[GoogleSheetManager] Sprite 라벨 없음: {spriteKey}");
continue;
}
var handle = Addressables.LoadAssetAsync<Sprite>(spriteKey);
await handle.Task;
if (handle.Status != AsyncOperationStatus.Succeeded || handle.Result == null)
{
Debug.LogWarning($"[GoogleSheetManager] Sprite 로드 실패 또는 null: {spriteKey}");
continue;
}
value = handle.Result;
}
else if ((field?.FieldType.IsEnum ?? false) || (property?.PropertyType.IsEnum ?? false))
if ((field?.FieldType.IsEnum ?? false) || (property?.PropertyType.IsEnum ?? false))
{
Type enumType = field?.FieldType ?? property?.PropertyType;
string formatted = NormalizeEnumKey(prop.Value.ToString());
@ -758,7 +734,6 @@ private async Task<bool> InternalCreateGoogleSheetSoAsync()
list.Add(dataInstance);
}
// ✅ SetDataList() 호출
MethodInfo setMethod = soType.GetMethod("SetDataList",
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
if (setMethod != null)
@ -811,7 +786,13 @@ private string NormalizeEnumKey(string input)
// 첫 글자 대문자화
return char.ToUpper(validName[0]) + validName.Substring(1);
}
private string NormalizeSpriteKey(string name)
{
if (string.IsNullOrEmpty(name)) return "";
return name.Replace("(Clone)", "").Trim();
}
private void OnValidate()
{
if (_refreshTrigger && !_alreadyCreatedSo && EditorPrefs.GetBool("GoogleSheetManager_ShouldCreateSO"))

View File

@ -30,7 +30,7 @@ public async void Initialize(string id)
{
var spriteRenderer = _visualLook.AddComponent<SpriteRenderer>();
_renderer = spriteRenderer;
spriteRenderer.sprite = environmentData.Sprite;
spriteRenderer.sprite = DataManager.Instance.GetSprite(environmentData.SpriteKey);
spriteRenderer.sortingOrder = 5;
Material material = await AssetManager.LoadAsset<Material>(DataConstants.BasePropSpriteMaterial);
spriteRenderer.material = new Material(material);

View File

@ -12,9 +12,9 @@ public static class DataConstants
public const string ItemDataSo = "ItemDataSo";
public const string FoodDataSo = "FoodDataSo";
public const string EnvironmentDataSo = "EnvironmentDataSo";
public const string SpriteDataSo = "SpriteDataSo";
public const string RestaurantPlayerDataSo = "RestaurantPlayerDataSo";
public const string AtlasLabel = "Atlas";
public const string BasePropSpriteMaterial = "BasePropSpriteMaterial";
}