using System; using System.Collections.Generic; using System.Threading.Tasks; using UnityEngine; using UnityEngine.U2D; namespace DDD { public class DataManager : Singleton, IManager { private Dictionary _dataAssetTable = new(); private Dictionary _spriteAtlas; private const string AssetLabel = "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 assets = await AssetManager.Instance.LoadAssetsByLabel(AssetLabel); _dataAssetTable = new Dictionary(assets.Count); foreach (var asset in assets) { var type = asset.GetType(); _dataAssetTable.TryAdd(type, asset); } Debug.Log($"[DataManager] {_dataAssetTable.Count}개의 DataAsset이 로드되었습니다."); } private async Task LoadSpriteAtlas() { List spriteAtlases = await AssetManager.Instance.LoadAssetsByLabel(DataConstants.AtlasLabel); _spriteAtlas = new Dictionary(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 GetDataAsset() where T : ScriptableObject { if (_dataAssetTable.TryGetValue(typeof(T), out var so)) { return so as T; } Debug.LogError($"[DataManager] {typeof(T).Name}을 찾을 수 없습니다."); return null; } public Sprite GetSprite(string key) => _spriteAtlas.GetValueOrDefault(key); public Sprite GetIcon(string key) => GetSprite(key + Icon); } }