Practice_Unity/Assets/Scripts/Managers/DataManager.cs
SEAN-59 af081c5975 작업
1. 데이터 매니저 변경
- 기존에 Dictionary 방식으로 만 처리가 되게 만들어 리스트 형태의 JSON이 아닌 그냥 단순 심플 형태의 JSON은 값 읽기 위해서는 JSON 자체를 변경했어야 했는데 이러한 문제 변경
2. 플레이어 컨트롤러에서 데이터 읽어서 플레이어 상태 관련 수정
- 플레이어 컨트롤러, 플레이어 데이터, 플레이어 데이터.json 수정

Todo
1. DataManager 이거 동작 원리 확실하게 정리를 해둬야 할거 같음 볼때마다 헷갈리면 만든 의의가 없음
2. 이제 실질적으로 몬스터 AI를 만들기 시작할 것
3. 캐릭터가 공격할때 사용할 방법에 대해서 생각 해 볼 것
2025-09-28 23:40:01 +09:00

73 lines
2.3 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
public interface ILoader<T>
{
List<T> GetList() { throw new Exception("NOT IMPLEMENT"); }
T GetSingle() { throw new Exception("NOT IMPLEMENT"); }
}
public class DataManager : IManager
{
private Dictionary<string, object> _cache = new Dictionary<string, object>();
public Dictionary<TKey, TValue> LoadToDict<TLoader, TKey, TValue>(string path, System.Func<TValue, TKey> keySelector) where TLoader : ILoader<TValue>
{
if (_cache.TryGetValue(path, out object cachedDict)) return (Dictionary<TKey, TValue>)cachedDict;
TLoader load = LoadJson<TLoader, TValue>(path);
List<TValue> list = load.GetList();
Dictionary<TKey, TValue> dict = new Dictionary<TKey, TValue>();
foreach (TValue item in list)
{
dict.Add(keySelector(item), item);
}
_cache.Add(path, dict);
return dict;
}
public T LoadSingle<TLoader, T>(string path) where TLoader : ILoader<T>
{
if (_cache.TryGetValue(path, out object cachedItem)) return (T)cachedItem;
// List<T> list = LoadJson<TLoader, T>(path);
// if (list.Count == 0) throw new Exception("HAVE NOT ITEM");
// if (list.Count > 1) throw new Exception("MANY ITEM");
//
// T item = list[0];
// _cache.Add(path, item);
// LoadJson을 호출하는 대신, 아래와 같이 직접 로드하고 GetSingle()을 호출합니다.
TLoader load = LoadJson<TLoader, T>(path);
T single = load.GetSingle();
_cache.Add(path, single);
return single;
}
// private List<T> LoadJson<TLoader, T>(string path) where TLoader : ILoader<T>
// {
// TextAsset textAsset = Manager.Resource.Load<TextAsset>($"{path}");
// TLoader data = JsonUtility.FromJson<TLoader>(textAsset.text);
// return data.GetList();
// }
private TLoader LoadJson<TLoader, T>(string path) where TLoader : ILoader<T>
{
TextAsset textAsset = Manager.Resource.Load<TextAsset>($"{path}");
TLoader data = JsonUtility.FromJson<TLoader>(textAsset.text);
return data;
}
public void Clear()
{
_cache.Clear();
}
}