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

85 lines
2.6 KiB
C#
Raw Normal View History

2025-07-10 05:48:44 +00:00
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
2025-07-09 09:45:11 +00:00
using UnityEngine;
using UnityEngine.U2D;
2025-07-09 09:45:11 +00:00
namespace DDD
{
public class DataManager : Singleton<DataManager>, IManager
{
2025-08-28 11:00:22 +00:00
private Dictionary<Type, ScriptableObject> _dataAssetTable = new();
private Dictionary<string, Sprite> _spriteAtlas;
2025-08-28 11:00:22 +00:00
private const string AssetLabel = "GoogleSheetSo";
private const string Icon = "_icon";
public void PreInit()
2025-07-09 09:45:11 +00:00
{
}
public async Task Init()
2025-07-09 09:45:11 +00:00
{
await LoadAllGameDataSo();
await LoadSpriteAtlas();
}
public void PostInit()
{
}
private async Task LoadAllGameDataSo()
{
2025-08-28 11:00:22 +00:00
var assets = await AssetManager.Instance.LoadAssetsByLabel<ScriptableObject>(AssetLabel);
_dataAssetTable = new Dictionary<Type, ScriptableObject>(assets.Count);
foreach (var asset in assets)
{
2025-08-28 11:00:22 +00:00
var type = asset.GetType();
_dataAssetTable.TryAdd(type, asset);
}
2025-08-28 11:00:22 +00:00
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);
}
2025-07-10 05:48:44 +00:00
}
}
2025-08-28 11:00:22 +00:00
public T GetDataAsset<T>() where T : ScriptableObject
{
2025-08-28 11:00:22 +00:00
if (_dataAssetTable.TryGetValue(typeof(T), out var so))
{
return so as T;
}
2025-08-28 11:00:22 +00:00
Debug.LogError($"[DataManager] {typeof(T).Name}을 찾을 수 없습니다.");
return null;
}
public Sprite GetSprite(string key) => _spriteAtlas.GetValueOrDefault(key);
public Sprite GetIcon(string key) => GetSprite(key + Icon);
2025-07-09 09:45:11 +00:00
}
}