ProjectDDD/Assets/_DDD/_Scripts/GameData/DataManager.cs
NTG_Lenovo 4e1f5f1a2e GetIcon 기능 추가
(Environment의 이미지도 있으면서 ui의 이미지도 다른 경우)
2025-08-06 11:40:04 +09:00

85 lines
2.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.U2D;
namespace DDD
{
public class DataManager : Singleton<DataManager>, IManager
{
private readonly Dictionary<Type, ScriptableObject> _dataSoTable = new();
private Dictionary<string, Sprite> _spriteAtlas;
private const string SoLabel = "GoogleSheetSo";
private const string Icon = "_icon";
public void PreInit()
{
}
public async Task Init()
{
await LoadAllGameDataSo();
await LoadSpriteAtlas();
}
public void PostInit()
{
}
private async Task LoadAllGameDataSo()
{
var soList = await AssetManager.LoadAssetsByLabel<ScriptableObject>(SoLabel);
foreach (var so in soList)
{
var type = so.GetType();
_dataSoTable.TryAdd(type, so);
}
Debug.Log($"[DataManager] {_dataSoTable.Count}개의 SO가 로드되었습니다.");
}
private async Task LoadSpriteAtlas()
{
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(CommonConstants.Clone, string.Empty).Trim();
_spriteAtlas.TryAdd(key, sprite);
}
}
}
public T GetDataSo<T>() where T : ScriptableObject
{
if (_dataSoTable.TryGetValue(typeof(T), out var so))
{
return so as T;
}
Debug.LogError($"[DataManager] {typeof(T).Name} SO를 찾을 수 없습니다.");
return null;
}
public Sprite GetSprite(string key) => _spriteAtlas.GetValueOrDefault(key);
public Sprite GetIcon(string key) => GetSprite(key + Icon);
}
}