using System.Collections; using System.Collections.Generic; using UnityEngine; using LitJson; using System.IO; public class DataManager<T> { private string filePath; private static DataManager<T> instance; private DataManager() { filePath = Application.streamingAssetsPath + "/data.json"; } public static DataManager<T> GetInstance() { if (null == instance) { instance = new DataManager<T>(); } return instance; } public List<T> LoadAll() { string str = File.ReadAllText(filePath); T[] rows = JsonMapper.ToObject<T[]>(str); if(rows.Length>0) { return new List<T>(rows); } return null; } public void Save(List<T> data) { string str = JsonMapper.ToJson(data); File.WriteAllText(filePath, str); } }
原文:https://www.cnblogs.com/clhxxlcj/p/10916759.html