using System; using System.Collections.Generic; using UnityEngine; public interface ILoader { List GetList(); } 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; List list = LoadJson(path); Dictionary dict = new Dictionary(); foreach (TValue item in list) { dict.Add(keySelector(item), item); } _cache.Add(path, dict); return dict; } private List LoadJson(string path) where TLoader : ILoader { TextAsset textAsset = Manager.Resource.Load($"{path}"); TLoader data = JsonUtility.FromJson(textAsset.text); return data.GetList(); } public void Clear() { _cache.Clear(); } }