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

77 lines
2.5 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-07-10 05:48:44 +00:00
public ItemDataSo ItemDataSo { get; private set; }
public FoodDataSo FoodDataSo { get; private set; }
public EnvironmentDataSo EnvironmentDataSo { get; private set; }
private Dictionary<string, Sprite> _spriteAtlas;
public bool IsInitialized { get; private set; }
2025-07-10 05:48:44 +00:00
2025-07-09 09:45:11 +00:00
public void Init()
{
}
2025-07-10 05:48:44 +00:00
public async void PostInit()
2025-07-09 09:45:11 +00:00
{
2025-07-10 05:48:44 +00:00
try
{
ItemDataSo = await AssetManager.LoadAsset<ItemDataSo>(DataConstants.ItemDataSo);
FoodDataSo = await AssetManager.LoadAsset<FoodDataSo>(DataConstants.FoodDataSo);
EnvironmentDataSo = await AssetManager.LoadAsset<EnvironmentDataSo>(DataConstants.EnvironmentDataSo);
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;
2025-07-17 02:15:19 +00:00
var key = sprite.name.Replace(CommonConstants.Clone, string.Empty).Trim();
_spriteAtlas.TryAdd(key, sprite);
}
}
IsInitialized = true;
2025-07-10 05:48:44 +00:00
}
catch (Exception e)
{
Debug.LogError($"So bind failed\n{e.Message}");
}
2025-07-09 09:45:11 +00:00
}
public async Task WaitUntilInitialized()
{
while (!IsInitialized)
{
await Task.Yield();
}
}
public Sprite GetSprite(string key) => _spriteAtlas.GetValueOrDefault(key);
2025-07-10 05:48:44 +00:00
// TODO : So가 늘어나는 경우 관리 방법 변경 필요성이 있음
2025-07-09 09:45:11 +00:00
// GetItemType(id)
// GetItemImage
// GetItemName
}
}