77 lines
2.5 KiB
C#
77 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
|
|
{
|
|
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; }
|
|
|
|
public void Init()
|
|
{
|
|
|
|
}
|
|
|
|
public async void PostInit()
|
|
{
|
|
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;
|
|
|
|
var key = sprite.name.Replace(CommonConstants.Clone, string.Empty).Trim();
|
|
_spriteAtlas.TryAdd(key, sprite);
|
|
}
|
|
}
|
|
|
|
IsInitialized = true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogError($"So bind failed\n{e.Message}");
|
|
}
|
|
}
|
|
|
|
public async Task WaitUntilInitialized()
|
|
{
|
|
while (!IsInitialized)
|
|
{
|
|
await Task.Yield();
|
|
}
|
|
}
|
|
|
|
public Sprite GetSprite(string key) => _spriteAtlas.GetValueOrDefault(key);
|
|
|
|
// TODO : So가 늘어나는 경우 관리 방법 변경 필요성이 있음
|
|
// GetItemType(id)
|
|
// GetItemImage
|
|
// GetItemName
|
|
}
|
|
} |