/// <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 = JsonConvert <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.Exception("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> /// 读取打包资源配置信息. /// </summary> /// <returns>打包资源配置信息.</returns> /// <param name="iDirPath">Asset存放目录文件(不指定:当前选定对象所在目录).</param> public static T ReadSetting <T>(string iDirPath = null) where T : UnityEngine.ScriptableObject { T objRet = default(T); string path = null; try { path = GetAssetFilePath <T>(iDirPath); if (true == string.IsNullOrEmpty(path)) { UtilsLog.Error("UtilityAsset", "GetAssetFilePath():Failed!!!(Dir:{0})", true == string.IsNullOrEmpty(iDirPath) ? "null" : iDirPath); return(objRet); } UtilsLog.Info("UtilityAsset", "ReadSetting:{0}", path); objRet = UtilsAsset.LoadAssetFile <T>(path); SetAssetDirty(objRet); } catch (System.IO.DirectoryNotFoundException exp) { UtilsLog.Exception("UtilityAsset", "ReadSetting Failed!!! DetailInfo ClassName:{0} \n AssetFile:{1} \n Error:{2}", typeof(T).ToString(), (path == null) ? "null" : path, exp.Message); } if (default(T) == objRet) { objRet = UtilsAsset.CreateAsset <T> (iDirPath); AssetsRefresh(); SetAssetDirty(objRet); } return(objRet); }
/// <summary> // 创建Asset. /// </summary> /// <param name="iDirPath">创建目录.</param> public static T CreateAsset <T> (string iDirPath = null) where T : UnityEngine.ScriptableObject { T objRet = default(T); #if UNITY_EDITOR string assetFullPath = null; try { assetFullPath = GetAssetFilePath <T> (iDirPath); if (assetFullPath.StartsWith("Resources/") == true) { assetFullPath = string.Format("{0}/{1}", _dataPath, assetFullPath); } if (assetFullPath.EndsWith(".asset") == false) { assetFullPath = string.Format("{0}.asset", assetFullPath); } if (File.Exists(assetFullPath) == true) { File.Delete(assetFullPath); } assetFullPath = assetFullPath.Replace(_dataPath, "Assets"); assetFullPath = AssetDatabase.GenerateUniqueAssetPath(assetFullPath); T asset = ScriptableObject.CreateInstance <T> (); AssetDatabase.CreateAsset(asset, assetFullPath); AssetsRefresh(); EditorUtility.FocusProjectWindow(); Selection.activeObject = asset; if (File.Exists(assetFullPath) == true) { UtilsLog.Info("UtilityAsset", "CreateAsset Successed!!!(File:{0})", assetFullPath); // 读取并返回创建对象实例 objRet = AssetDatabase.LoadAssetAtPath <T> (assetFullPath); } else { UtilsLog.Error("UtilityAsset", "CreateAsset Failed!!!(File:{0})", assetFullPath); } } catch (UnityException exp) { UtilsLog.Exception("UtilityAsset", "CreateAsset Failed!!! DetailInfo ClassName:{0} \n AssetFile:{1} \n Error:{2}", typeof(T).ToString(), (assetFullPath == null) ? "null" : assetFullPath, exp.Message); } #endif if (objRet != null) { return(objRet); } else { return(default(T)); } }
/// <summary> /// 日志输出:异常. /// </summary> /// <param name="iScript">脚本.</param> /// <param name="iFormat">格式.</param> /// <param name="iArgs">参数.</param> public void Exception(string iFormat, params object[] iArgs) { #if BUILD_DEBUG if (false == this._logOutput) { return; } UtilsLog.Exception(this.ClassName, iFormat, iArgs); #endif }
/// <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 = JsonConvert <T> .ConvertFromJsonString(jsonString); UtilsLog.Info("UtilityAsset", "ImportDataByPath. <- Path:{0}", iJsonFilePath); UtilsLog.Info("UtilityAsset", "ImportDataByPath. <- Data:{0}", jsonString); } } catch (System.Exception exp) { UtilsLog.Exception("UtilityAsset", "ImportData 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); }
/// <summary> /// 日志输出:异常. /// </summary> /// <param name="iScript">脚本.</param> /// <param name="iFormat">格式.</param> /// <param name="iArgs">参数.</param> public void Exception(string iFormat, params object[] iArgs) { #if BUILD_DEBUG UtilsLog.Exception(this.ClassName, iFormat, iArgs); #endif }