void Awake(){//检测程序是否关闭 2018版本以上的Application.wantsToQuit += () =>{string consoleLogPath = Application.consoleLogPath; //日志路径if (File.Exists(consoleLogPath)){string filePath = Path.GetDirectoryName(consoleLogPath);string fileExtension = Path.GetExtension(consoleLogPath);string fileName = Path.GetFileNameWithoutExtension(consoleLogPath);string OutLogFilePath = Application.dataPath + "/../LogOut/" + fileName + "_" + DateTime.Now.ToString("yyyyMMddHHmm") + fileExtension;string NewfilePath = Path.GetDirectoryName(OutLogFilePath);if (Directory.Exists(NewfilePath) == false){Directory.CreateDirectory(NewfilePath);}File.Copy(consoleLogPath, OutLogFilePath, true);//当文件满足六个时,保留时间最新的三个文件DirectoryInfo dir = new DirectoryInfo(NewfilePath);if (dir.GetFiles().Length > 5){string logFileType = "*.log";string[] filePathArr = Directory.GetFiles(NewfilePath, logFileType, SearchOption.TopDirectoryOnly);Dictionary<string, DateTime> fileCreateDate = new Dictionary<string, DateTime>();if (filePathArr.Length == 0){logFileType = "*.txt";filePathArr = Directory.GetFiles(NewfilePath, logFileType, SearchOption.TopDirectoryOnly);}for (int i = 0; i < filePathArr.Length; i++){FileInfo fi = new FileInfo(filePathArr[i]);fileCreateDate[filePathArr[i]] = fi.CreationTime;}fileCreateDate = fileCreateDate.OrderBy(f => f.Value).ToDictionary(f => f.Key, f => f.Value);foreach (KeyValuePair<string, DateTime> item in fileCreateDate){if (File.Exists(item.Key) && Directory.GetFiles(NewfilePath, logFileType, SearchOption.TopDirectoryOnly).Length > 3){File.Delete(item.Key);}}}}return true;};}/// <summary>/// 程序关闭(正常)2017版本以下的(不包括右键关闭exe程序)/// </summary>void OnApplicationQuit(){string consoleLogPath = Application.consoleLogPath; //日志路径if (File.Exists(consoleLogPath)){string filePath = Path.GetDirectoryName(consoleLogPath);string fileExtension = Path.GetExtension(consoleLogPath);string fileName = Path.GetFileNameWithoutExtension(consoleLogPath);string OutLogFilePath = Application.dataPath + "/../LogOut/" + fileName + "_" + DateTime.Now.ToString("yyyyMMddHHmm") + fileExtension;string NewfilePath = Path.GetDirectoryName(OutLogFilePath);if (Directory.Exists(NewfilePath) == false){Directory.CreateDirectory(NewfilePath);}File.Copy(consoleLogPath, OutLogFilePath, true);//当文件满足六个时,保留时间最新的三个文件DirectoryInfo dir = new DirectoryInfo(NewfilePath);if (dir.GetFiles().Length > 5){string logFileType = "*.log";string[] filePathArr = Directory.GetFiles(NewfilePath, logFileType, SearchOption.TopDirectoryOnly);Dictionary<string, DateTime> fileCreateDate = new Dictionary<string, DateTime>();if (filePathArr.Length == 0){logFileType = "*.txt";filePathArr = Directory.GetFiles(NewfilePath, logFileType, SearchOption.TopDirectoryOnly);}for (int i = 0; i < filePathArr.Length; i++){FileInfo fi = new FileInfo(filePathArr[i]);fileCreateDate[filePathArr[i]] = fi.CreationTime;}fileCreateDate = fileCreateDate.OrderBy(f => f.Value).ToDictionary(f => f.Key, f => f.Value);foreach (KeyValuePair<string, DateTime> item in fileCreateDate){if (File.Exists(item.Key) && Directory.GetFiles(NewfilePath, logFileType, SearchOption.TopDirectoryOnly).Length > 3){File.Delete(item.Key);}}}}}
