using System; using System.Collections.Generic; using UnityEngine; public interface ILoader { List GetList() { throw new Exception("NOT IMPLEMENT"); } T GetSingle() { throw new Exception("NOT IMPLEMENT"); } } public class DataManager : IManager { private Dictionary _cache = new Dictionary(); public Dictionary LoadToDict(string path, System.Func keySelector) where TLoader : ILoader { if (_cache.TryGetValue(path, out object cachedDict)) return (Dictionary)cachedDict; TLoader load = LoadJson(path); List list = load.GetList(); Dictionary dict = new Dictionary(); foreach (TValue item in list) { dict.Add(keySelector(item), item); } _cache.Add(path, dict); return dict; } public T LoadSingle(string path) where TLoader : ILoader { if (_cache.TryGetValue(path, out object cachedItem)) return (T)cachedItem; // List list = LoadJson(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(path); T single = load.GetSingle(); _cache.Add(path, single); return single; } // private List LoadJson(string path) where TLoader : ILoader // { // TextAsset textAsset = Manager.Resource.Load($"{path}"); // TLoader data = JsonUtility.FromJson(textAsset.text); // return data.GetList(); // } private TLoader LoadJson(string path) where TLoader : ILoader { TextAsset textAsset = Manager.Resource.Load($"{path}"); TLoader data = JsonUtility.FromJson(textAsset.text); return data; } public void Clear() { _cache.Clear(); } }