/// <summary> /// 清空目录. /// </summary> /// <param name="iDir">目录.</param> public static void ClearDirectory(string iDir) { // 清空文件 string[] files = Directory.GetFiles(iDir); if ((null != files) && (1 <= files.Length)) { foreach (string file in files) { File.Delete(file); UtilsLog.Info("UtilityAsset", "ClearDirectory Delete File:{0}", file); } } // 清空子目录 string[] dirs = Directory.GetDirectories(iDir); if ((null != dirs) && (1 <= dirs.Length)) { foreach (string dir in dirs) { ClearDirectory(dir); } } else { Directory.Delete(iDir); UtilsLog.Info("UtilityAsset", "ClearDirectory Delete Directory:{0}", iDir); } }
/// <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.Fatal("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> /// 导出成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> /// 拷贝目录. /// </summary> /// <param name="iFromDir">源目录.</param> /// <param name="iToDir">目标目录.</param> public static void CopyDirectory(string iFromDir, string iToDir) { DirectoryInfo dirInfo = new DirectoryInfo(iFromDir); string _toDir = string.Format("{0}/{1}", iToDir, dirInfo.Name); if (false == Directory.Exists(_toDir)) { Directory.CreateDirectory(_toDir); } // 拷贝文件 FileInfo[] allFiles = dirInfo.GetFiles(); foreach (FileInfo file in allFiles) { if (true == file.Name.EndsWith(".meta")) { continue; } // 拷贝文件 string copyToFile = string.Format("{0}/{1}", _toDir, file.Name); UtilsLog.Info("UtilityAsset", "CopyDirectory File: {0} -> {1}", file.FullName, copyToFile); File.Copy(file.FullName, copyToFile, true); } // 检索子文件夹 DirectoryInfo[] subDirs = dirInfo.GetDirectories(); foreach (DirectoryInfo subDir in subDirs) { string _fromDir = string.Format("{0}/{1}", iFromDir, subDir.Name); CopyDirectory(_fromDir, _toDir); } }
/// <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.Fatal("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> protected virtual void OnApplyClick() { UtilsLog.Info(this.ClassName, "OnApplyClick"); // 先导出保存 this.ExportToJsonFile(); // 应用 this.Apply(); }
/// <summary> /// 日志输出:警告. /// </summary> /// <param name="iScript">脚本.</param> /// <param name="iFormat">格式.</param> /// <param name="iArgs">参数.</param> public void Warning(string iFormat, params object[] iArgs) { #if BUILD_DEBUG if (false == this._logOutput) { return; } UtilsLog.Warning(this.ClassName, iFormat, iArgs); #endif }
/// <summary> /// 导出成JSON文件. /// </summary> /// <returns>导出文件(Json格式).</returns> /// <param name="iExportDir">导出路径.</param> public virtual string ExportToJsonFile(string iExportDir) { string exportDir = iExportDir; if (true == string.IsNullOrEmpty(exportDir)) { exportDir = this.JsonPath; } if (false == UtilsAsset.CheckAndCreateDirByFullPath(this.JsonPath)) { UtilsLog.Error(this.ClassName, "ExportToJsonFile -> CheckAndCreateDirByFullPath Failed!!! \n (Path:{0})", this.JsonPath); return(null); } return(UtilsAsset.ExportData <T1> (this.Data, exportDir)); }
/// <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); }
/// <summary> /// 取得实例. /// </summary> /// <returns>实例.</returns> /// <param name="iPath">读取路径.</param> public static T1 GetInstance(string iPath = null) { if (_instance == null) { _instance = UtilsAsset.ReadSetting <T1>(iPath); string _name = typeof(T1).Name; if ((null != _instance) && (true == _instance.Init())) { UtilsLog.Info(_name, "GetInstance Successed!!!"); } else { UtilsLog.Error(_name, "GetInstance Failed!!!"); return(null); } } return(_instance); }
public static bool CheckAndCreateDir(string iDir) { if (true == string.IsNullOrEmpty(iDir)) { return(true); } if (false == Directory.Exists(iDir)) { UtilsLog.Info("UtilityAsset", "CheckAndCreateDir():Dir:{0}", iDir); Directory.CreateDirectory(iDir); if (false == Directory.Exists(iDir)) { UtilsLog.Error("UtilityAsset", "CheckAndCreateDir():Dir Failed!!!(Dir:{0})", iDir); return(false); } } return(true); }
/// <summary> /// 初始化数据. /// </summary> /// <returns><c>true</c>, OK, <c>false</c> NG.</returns> public override bool InitAsset() { // 打包ID if (string.IsNullOrEmpty(this.Data.BuildID) == false) { #if UNITY_EDITOR #if UNITY_5_5_OR_NEWER PlayerSettings.applicationIdentifier = this.Data.BuildID; #else PlayerSettings.bundleIdentifier = this.Data.BuildID; #endif #endif } // 版本号 if (string.IsNullOrEmpty(this.Data.BuildVersion) == false) { #if UNITY_EDITOR && UNITY_EDITOR PlayerSettings.bundleVersion = this.Data.BuildVersion; #endif } // 版本号 if (-1 != this.Data.BuildVersionCode) { #if UNITY_IOS && UNITY_EDITOR PlayerSettings.iOS.buildNumber = this.BuildVersionCode.ToString(); #endif #if UNITY_ANDROID && UNITY_EDITOR PlayerSettings.Android.bundleVersionCode = this.Data.BuildVersionCode; #endif } UtilsLog.Info("BuildInfo", "BuildName : {0}", (this.Data.BuildName == null) ? "null" : this.Data.BuildName); UtilsLog.Info("BuildInfo", "BuildID : {0}", (this.Data.BuildID == null) ? "null" : this.Data.BuildID); UtilsLog.Info("BuildInfo", "BuildVersion : {0}", (this.Data.BuildVersion == null) ? "null" : this.Data.BuildVersion); UtilsLog.Info("BuildInfo", "BuildShortVersion : {0}", (this.Data.BuildShortVersion == null) ? "null" : this.Data.BuildShortVersion); UtilsLog.Info("BuildInfo", "BuildVersionCode : {0}", this.Data.BuildVersionCode); UtilsLog.Info("BuildInfo", "CenterVersion : {0}", (this.Data.CenterVersion == null) ? "null" : this.Data.CenterVersion); return(true); }
/// <summary> /// 取得实例. /// </summary> /// <returns>实例.</returns> /// <param name="iPath">读取路径.</param> public static T1 GetInstance(string iPath = null) { if (_instance == null) { _instance = UtilsAsset.ReadSetting <T1>(iPath); string _name = typeof(T1).Name; if ((null != _instance) && (true == _instance.Init())) { UtilsLog.Info(_name, "GetInstance()::Successed!!!(Path:{0})", (true == string.IsNullOrEmpty(iPath)) ? "null" : iPath); } else { UtilsLog.Error(_name, "GetInstance()::Failed!!!(Path:{0})", (true == string.IsNullOrEmpty(iPath)) ? "null" : iPath); return(null); } } return(_instance); }
/// <summary> /// 检测匹配路径(始终控制在Application.dataPath之下). /// </summary> /// <returns>检测后的路径.</returns> /// <param name="iTargetPath">检测并取得匹配路径.</param> public static string CheckMatchPath(string iTargetPath) { string _targetPath = iTargetPath; string _rootPath = Application.dataPath; DirectoryInfo _targetDir = new DirectoryInfo(_targetPath); DirectoryInfo _rootDir = new DirectoryInfo(_rootPath); DirectoryInfo _targetParent = _targetDir.Parent; string _checkPath = string.Format("{0}", _targetDir.Name); while (null != _targetParent) { string _nameTmp = _targetParent.Name; _nameTmp = _nameTmp.Replace("\\", "/"); if (true == _nameTmp.EndsWith(":/")) { _checkPath = string.Format("{0}{1}", _nameTmp, _checkPath); } else if (true == _nameTmp.Equals("/")) { _checkPath = string.Format("{0}{1}", _nameTmp, _checkPath); } else { _checkPath = string.Format("{0}/{1}", _nameTmp, _checkPath); } if (true == _targetParent.Name.Equals(_rootDir.Name)) { _targetParent = null; } else { _targetParent = _targetParent.Parent; } } UtilsLog.Info("UtilityAsset", "CheckMatchPath():{0} \n -> {1}", iTargetPath, _checkPath); return(_checkPath); }
/// <summary> /// 创建时间计数器. /// </summary> /// <param name="iMaxValue">计数器最大值.</param> /// <param name="iOnCountOver">超过计时回调函数.</param> /// <param name="iMode">模式(默认为:倒计时).</param> public static TimeCounter Create( float iMaxValue, System.Action iOnCountOver = null, TCounterMode iMode = TCounterMode.CountDown) { TimeCounter objRet = new TimeCounter(); if (objRet != null) { objRet.InitCounter(iMaxValue, TCounterType.TimeCounter, iMode); if (iOnCountOver != null) { objRet.OnCountOver += iOnCountOver; } return(objRet); } else { UtilsLog.Error("TimeCounter", "Create()::TimeCounter Create Failed!!!"); return(null); } }
/// <summary> /// 加载(*.asset)文件. /// * 加载优先顺序 /// 1)编辑器模式加载 /// 2)Resource下的资源 /// </summary> /// <returns>加载文件对象.</returns> /// <param name="iPath">路径.</param> /// <typeparam name="T">加载对象类型.</typeparam> public static T LoadAssetFile <T>(string iPath) where T : Object { T objRet = default(T); TextAsset textAsset = null; // 1)编辑器模式加载 #if UNITY_EDITOR objRet = AssetDatabase.LoadAssetAtPath <T>(iPath); if (objRet != default(T)) { return(objRet); } else { UtilsLog.Warning("UtilityAsset", "LoadAssetFile Failed!!! Path : {0}", iPath); } #endif // 2)Resource下的资源 // 若上述找不到,则在从资源文件夹中加载 const string _key = "Resources/"; if ((textAsset == null) && (iPath.Contains(_key) == true)) { int startIndex = iPath.IndexOf(_key); startIndex += _key.Length; string pathTmp = iPath.Substring(startIndex); int endIndex = pathTmp.LastIndexOf("."); if (-1 != endIndex) { pathTmp = pathTmp.Substring(0, endIndex); } Object objTemp = Resources.Load(pathTmp); objRet = objTemp as T; return(objRet); } return(default(T)); }
/// <summary> /// 信息日志(默认:运行日志). /// </summary> /// <param name="iScript">脚本.</param> /// <param name="iFormat">格式.</param> /// <param name="iArgs">参数.</param> public void Info(string iFormat, params object[] iArgs) { UtilsLog.Info(this.ClassName, iFormat, iArgs); }
/// <summary> /// Debug日志. /// </summary> /// <param name="iScript">脚本.</param> /// <param name="iFormat">格式.</param> /// <param name="iArgs">参数.</param> public void DebugLog(string iFormat, params object[] iArgs) { UtilsLog.DebugLog(this.ClassName, iFormat, iArgs); }
/// <summary> /// 致命日志. /// </summary> /// <param name="iScript">脚本.</param> /// <param name="iFormat">格式.</param> /// <param name="iArgs">参数.</param> public void Fatal(string iFormat, params object[] iArgs) { UtilsLog.Fatal(this.ClassName, iFormat, iArgs); }
/// <summary> /// 错误日志. /// </summary> /// <param name="iScript">脚本.</param> /// <param name="iFormat">格式.</param> /// <param name="iArgs">参数.</param> public void Error(string iFormat, params object[] iArgs) { UtilsLog.Error(this.ClassName, iFormat, iArgs); }
/// <summary> /// 警告日志. /// </summary> /// <param name="iScript">脚本.</param> /// <param name="iFormat">格式.</param> /// <param name="iArgs">参数.</param> public void Warning(string iFormat, params object[] iArgs) { UtilsLog.Warning(this.ClassName, iFormat, iArgs); }
/// <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 }
/// <summary> /// 日志输出:警告. /// </summary> /// <param name="iScript">脚本.</param> /// <param name="iFormat">格式.</param> /// <param name="iArgs">参数.</param> public void Warning(string iFormat, params object[] iArgs) { #if BUILD_DEBUG UtilsLog.Warning(this.ClassName, iFormat, iArgs); #endif }
/// <summary> /// 清空按钮点击事件. /// </summary> protected virtual void OnClearClick() { UtilsLog.Info(this.ClassName, "OnClearClick"); // 清空自定义宏一览 this.Clear(true); }
/// <summary> /// 导入按钮点击事件. /// </summary> protected virtual void OnImportClick() { UtilsLog.Info(this.ClassName, "OnImportClick"); this.ImportFromJsonFile(true); }
/// <summary> /// 导出按钮点击事件. /// </summary> protected virtual void OnExportClick() { UtilsLog.Info(this.ClassName, "OnExportClick"); this.ExportToJsonFile(); }
/// <summary> /// 初始化窗口尺寸信息. /// </summary> /// <param name="iDisplayRect">表示范围.</param> protected virtual void InitWindowSizeInfo(Rect iDisplayRect) { UtilsLog.Info(this.ClassName, "InitWindowSizeInfo Rect(X:{0} Y:{1} Width:{2} Height:{3})", iDisplayRect.x, iDisplayRect.y, iDisplayRect.width, iDisplayRect.height); }