ProjectDDD/Assets/_DDD/_Scripts/GameData/DataManager.cs

88 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";
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);
// TODO : So가 늘어나는 경우 관리 방법 변경 필요성이 있음
// GetItemType(id)
// GetItemImage
// GetItemName
}
}