ProjectDDD/Assets/_DDD/_Scripts/Game/GameData/DataManager.cs
2025-08-28 20:00:22 +09:00

85 lines
2.6 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 Dictionary<Type, ScriptableObject> _dataAssetTable = new();
private Dictionary<string, Sprite> _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<ScriptableObject>(AssetLabel);
_dataAssetTable = new Dictionary<Type, ScriptableObject>(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<SpriteAtlas> spriteAtlases = await AssetManager.Instance.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 GetDataAsset<T>() 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);
}
}