/// <summary> /// 导出成JSON文件. /// </summary> /// <returns>导出路径.</returns> /// <param name="iInstance">欲导出实例对象.</param> /// <param name="iJsonFileDir">导出Json文件目录.</param> /// <typeparam name="T">指定读取Asset文件绑定类.</typeparam> public static string ExportData <T>(T iInstance, string iJsonFileDir = null) where T : JsonDataBase, new() { string jsonFilePath = null; try { jsonFilePath = GetJsonFilePath <T>(iJsonFileDir); // 若已经有文件存在,则强制删除 if (File.Exists(jsonFilePath) == true) { File.Delete(jsonFilePath); } // 导出JSON文件 string jsonString = UtilsJson <T> .ConvertToJsonString(iInstance); File.WriteAllText(jsonFilePath, jsonString); UtilsLog.Info("UtilityAsset", "ExportData. -> Path:{0}", jsonFilePath); UtilsLog.Info("UtilityAsset", "ExportData. -> Data:{0}", jsonString); } catch (System.Exception exp) { UtilsLog.Fatal("UtilityAsset", "ExportData()::Failed!!! ClassName:{0} AssetFile:{1} Exception:{2} StackTrace:{3}", typeof(T).ToString(), (jsonFilePath == null) ? "null" : jsonFilePath, exp.Message, exp.StackTrace); } // AssetsRefresh(); if (true == File.Exists(jsonFilePath)) { UtilsLog.Info("UtilityAsset", "ExportData. -> {0}", jsonFilePath); } return(jsonFilePath); }
/// <summary> /// 从JSON文件,导入打包配置数据(文件必须存在). /// </summary> /// <param name="iJsonFileDir">导出Json文件目录.</param> /// <typeparam name="T">指定读取Asset文件数据类.</typeparam> public static T ImportDataByPath <T>(out bool iIsFileExist, string iJsonFilePath) where T : JsonDataBase, new() { T objRet = default(T); string jsonString = null; iIsFileExist = false; try { // 优先加载下载资源包中的信息 TextAsset _data = DataLoader.LoadData(iJsonFilePath) as TextAsset; if (null != _data) { // 读取文件 jsonString = _data.text; } else { // 若已经有文件不存在 if (File.Exists(iJsonFilePath) == false) { UtilsLog.Warning("UtilityAsset", "ImportDataByPath():File not exist!!![File:{0}]", iJsonFilePath); return(default(T)); } iIsFileExist = true; jsonString = File.ReadAllText(iJsonFilePath); } if (false == string.IsNullOrEmpty(jsonString)) { objRet = UtilsJson <T> .ConvertFromJsonString(jsonString); UtilsLog.Info("UtilityAsset", "ImportDataByPath. <- Path:{0}", iJsonFilePath); UtilsLog.Info("UtilityAsset", "ImportDataByPath. <- Data:{0}", jsonString); } } catch (System.Exception exp) { UtilsLog.Fatal("UtilityAsset", "ImportDataByPath()::Failed!!! \n ClassName:{0} \n AssetFile:{1} \n Exception:{2} \n StackTrace:{3}", typeof(T).ToString(), (iJsonFilePath == null) ? "null" : iJsonFilePath, exp.Message, exp.StackTrace); objRet = default(T); } return(objRet); }